JSONObject 文本必须以 1 处的 '{' 开头 [字符 2 行 1],在使用 HTTPClient 自动执行 API 时出现此错误



我正在使用HTTPCLIENT来自动化我的项目Web服务,并在获得csrf令牌后尝试登录,但我从响应中得到了这个错误,即使经过这么多次尝试,我也无法修复这个错误。

restClient = new RestClient();
HashMap<String, String> headerMaps = new HashMap<String, String>(); // Add header values
headerMaps.put("Content-Type", "application/json");
headerMaps.put("Connection", "keep-alive");
headerMaps.put("csrfToken", "test12");
// Jackson API : to convert java class object into JSON
ObjectMapper mapper = new ObjectMapper();
Users users = new Users("test@test.com", "abcd");
// Object to Jsonfile :mapper.writeValue(new 
File("C:/NewEclipse/Projectn/src/main/java/com/qa/data/users.json"),users);
//  Object to json in String:String usersJsonString = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(users);
System.out.println("Users json value is----> " + usersJsonString);
// Call the Post method
closeableHttpResponse = restClient.postLogin(url, usersJsonString,headerMaps);
String responseString = EntityUtils.toString(closeableHttpResponse.getEntity(), "UTF-8");
System.out.println("Value of RESPONSE STRING----> " + responseString);
JSONObject responseJson = new JSONObject(responseString); //Here I am getting error mentioned in subject

我得到的JSON值也是正确的:
{"电子邮件":"test@test.com&";,"密码":"abcd";}

hooray我自己找到了这个问题的解决方案,现在它运行良好

我们需要在标题中传递一个cookie,如下所示:

headerMaps.put("cookie","_csrf=value; connect.sid=value; csrfToken=value")

相关内容

  • 没有找到相关文章

最新更新