简体   繁体   English

如何在Java中获取没有转义序列的post请求的响应主体?

[英]How to get response body of a post request without escape sequences in Java?

I am trying to convert the HttpResponse's response body to a String using the following piece of code: 我正在尝试使用以下代码将HttpResponse的响应主体转换为String:

HttpGet request = new HttpGet(url);
request.setHeader("Accept", "application/json");
response = client.execute(request);
EntityUtils.toString(response.getEntity(), standardCharsets.UTF_8.displayName());

The expected response after converting it to a String is as follows: 将其转换为String后的预期响应如下:

{"errors": ["permission denied"]}

This code was working fine until recently. 这段代码直到最近才运行良好。 But now the response body is somehow being modified to this: 但现在响应主体以某种方式被修改为:

{"errors":["1 error occurred:\n\t* permission denied\n\n"]}

I do not understand how the "1 error occurred" message is being inserted into the response body as the response from the same call in Postman gives me the appropriate response ie: 我不明白如何将“1错误发生”消息插入到响应主体中,因为Postman中同一个调用的响应给了我适当的响应,即:

{"errors": ["permission denied"]}

And I've checked the code thoroughly and am certain that the "1 error occurred" message is not being inserted manually. 我已经彻底检查了代码,并确定没有手动插入“1错误发生”消息。

EDIT: 编辑:

These were the response headers from Postman: 这些是邮递员的回复标题:

cache-control →no-store
content-length →33
content-type →application/json
date →Thu, 30 May 2019 06:52:33 GMT
status →403

And these were the response headers from the HttpClient call: 这些是来自HttpClient调用的响应标头:

Cache-Control: no-store
Content-Type: application/json
Date: Thu, 30 May 2019 09:06:21 GMT
Content-Length: 60

The only difference is that the status code isn't being printed. 唯一的区别是没有打印状态代码。 But checking the status code through the response.getStatusLine().getStatusCode() yields 403 as well. 但是通过response.getStatusLine().getStatusCode()检查状态代码也会产生403。

Looks like you are using Apache HttpClient, which does not modify responses this way. 看起来您正在使用Apache HttpClient,它不会以这种方式修改响应。 It's definitely the response generated by server. 这绝对是服务器生成的响应。 Check your configuration of request in Postman to see if there are any additional headers, that may alter server behavior. 检查Postman中的请求配置,看看是否有任何其他标头可能会改变服务器行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM