httpClient post请求设置了Content-Type application/json还是无法转换为json格式
问题是这样的,开发时遇到了上述问题,我的代码是这么写的
nodejs 中的请求解析json数据引用
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
java:
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(apiUrl);
JSONObject obj = new JSONObject();
obj.put("filePath", "jpg");
obj.put("projectId", "abc");
StringEntity stringEntity = new StringEntity(obj.toString(), "UTF-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
CloseableHttpResponse response = httpClient.execute(httpPost);
int status = response.getStatusLine().getStatusCode()
运行结果
16:32:09.287 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "{"filePath":"jpg","projectId":"abc"}"
16:32:09.291 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 415 Unsupported Media Type[\r][\n]"
415 Unsupported Media Type 服务器无法处理请求附带的媒体格式
看样子json格式,没错,但是报415错误
于是我在请求头添加
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
依然报同样的错误,变成
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
于是把删掉
stringEntity.setContentEncoding(“UTF-8”); `
stringEntity.setContentType(“application/json”);
保留
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
就解决了
问题是解决了,但原因未明,我看其他博客
有时也会用到stringEntity.setContentType(“application/json”);
,
难道时header和stringEntity不能同时设置?
但只设置stringEntity又会包415,嗯,有点诡异
后来尝试了几种组合,注意utf-8后有分号
httpPost.addHeader("Content-Type", "application/json; charset=utf-8;");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
httpPost.addHeader("Content-Type", "application/json;");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
以上两种不报415,但是nodejs就没解析到
exports.xxx= (req, res) => {
logger.info('params:' + json(req.body));
2022-03-04T17:00:14+08:00 [info] : params:{}
看着时发过去了
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/json;[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Connection: keep-alive[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Length: 56[\r][\n]"
17:00:14.205 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Content-Encoding: UTF-8[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "Host: 127.0.0.1:8956[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.6 (Java/1.8.0_191)[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
17:00:14.206 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "{"filePath":"jpg","projectId":"abc"}"
httpPost.addHeader("Content-Type", "application/json");
stringEntity.setContentEncoding("UTF-8");
stringEntity.setContentType("application/json");
这个报415
嗯,记录一下