java—jsonobject文本必须以“{”开头,位于1[字符2第1行],在使用httpclient自动化api时出现此错误

q0qdq0h2  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(288)

我正在使用httpclient来自动化我的项目webservices,并且在获得csrf令牌后尝试登录,但是我从response中得到了这个错误,即使经过这么多的尝试,我也无法修复这个错误。

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“,”password“:”d“}

c2e8gylq

c2e8gylq1#

万岁,我找到了解决这个问题的办法,我自己,现在它的工作很好
解决方案:我们需要在标题中传递cookie,如下所示:
headermaps.put(“cookie”,“\u csrf=值;connect.sid=值;csrftoken=值“)

相关问题