如何使用 Apache HTTPClient 4.5 发送带有 JSON 参数的 POST。获取 415 不支持的媒体类型
How to send a POST with JSON parameters using Apache HTTPClient 4.5. Getting 415 unsupported media type
我正在尝试向我的服务器(使用 Apache HTTPClient 4.5)发送一个 POST 请求,其中包含一组 JSON 参数。我已经关注了一些 SO 问题,但是 运行 遇到了问题。
当我使用 javascript 控制台发送请求时,它有效!像这样:
//Using JS console, I send a POST request and it works.
$.post('/createConfigData', {
"tailSign": "A7ALE",
"active": "Y"
});
//Get back 201
当我使用 Apache HTTPClient 4.5 尝试做与上面相同的事情时,我得到 415 unsupported media type:
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setContentType("application/json;charset=utf-8");
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);
我从 working 请求 得到的数据是:
- 请求Headers:
- 主持人:"mysite.com"
- User-Agent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0"
- 接受:
"*/*"
- Accept-Language: "en-US,en;q=0.5"
- Accept-Encoding: "gzip, deflate"
- Content-Type: "application/x-www-form-urlencoded; charset=UTF-8"
- 推荐人:"mysite.com/index.html"
- Content-Length: "288"
- Cookie:"JSESSIONID=..."
- Proxy-Authorization: "Basic ..."
- 连接:"keep-alive"
- 编译指示:"no-cache"
- Cache-Control: "no-cache"
- 回复Headers:
- 年龄:“0”
- 连接:"Keep-Alive"
- Content-Type: "application/json;charset=UTF-8"
- 日期:"Wed, 19 Oct 2016 17:57:34 GMT"
- 服务器:"Apache-Coyote/1.1"
- Transfer-Encoding: "chunked"
我对在哪里设置内容类型有点困惑,无论是 entity 还是 httppost
=======================
更多测试用例
将 httppost 的 内容类型设置为 json 给我 400
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);
将 httppost 的 内容类型设置为 x-www-form-urlencoded 给我 415
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);
我也试过添加这一行:
httppost.addHeader("Accept", "*/*");
==================
解决方案
使用 wireshark 我发现有一条错误消息伴随着 400 错误请求!只是我的 JSON 不正确。
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 19 Oct 2016 21:33:25 GMT
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
{"code":400,"message":"Field ConfigName is Null, Invalid Value for Seat Count, Missing counts for DSU, ICMT, SVDU, TPMU, Login user details not found, Please enter valid lruData","fleetData":{"airlineData":null,"dimAircraftJson":null,"configData":null}}
您得到 415 不受支持的媒体类型 因为您已经将 content-type header 覆盖为 application/x-www-form-urlencoded;字符集=UTF-8。只需将其更改为:
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);
我正在尝试向我的服务器(使用 Apache HTTPClient 4.5)发送一个 POST 请求,其中包含一组 JSON 参数。我已经关注了一些 SO 问题,但是 运行 遇到了问题。
当我使用 javascript 控制台发送请求时,它有效!像这样:
//Using JS console, I send a POST request and it works.
$.post('/createConfigData', {
"tailSign": "A7ALE",
"active": "Y"
});
//Get back 201
当我使用 Apache HTTPClient 4.5 尝试做与上面相同的事情时,我得到 415 unsupported media type:
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setContentType("application/json;charset=utf-8");
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);
我从 working 请求 得到的数据是:
- 请求Headers:
- 主持人:"mysite.com"
- User-Agent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0"
- 接受:
"*/*"
- Accept-Language: "en-US,en;q=0.5"
- Accept-Encoding: "gzip, deflate"
- Content-Type: "application/x-www-form-urlencoded; charset=UTF-8"
- 推荐人:"mysite.com/index.html"
- Content-Length: "288"
- Cookie:"JSESSIONID=..."
- Proxy-Authorization: "Basic ..."
- 连接:"keep-alive"
- 编译指示:"no-cache"
- Cache-Control: "no-cache"
- 回复Headers:
- 年龄:“0”
- 连接:"Keep-Alive"
- Content-Type: "application/json;charset=UTF-8"
- 日期:"Wed, 19 Oct 2016 17:57:34 GMT"
- 服务器:"Apache-Coyote/1.1"
- Transfer-Encoding: "chunked"
我对在哪里设置内容类型有点困惑,无论是 entity 还是 httppost
=======================
更多测试用例
将 httppost 的 内容类型设置为 json 给我 400
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);
将 httppost 的 内容类型设置为 x-www-form-urlencoded 给我 415
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/x-www-form-urlencoded;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);
我也试过添加这一行:
httppost.addHeader("Accept", "*/*");
==================
解决方案
使用 wireshark 我发现有一条错误消息伴随着 400 错误请求!只是我的 JSON 不正确。
HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 19 Oct 2016 21:33:25 GMT
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
{"code":400,"message":"Field ConfigName is Null, Invalid Value for Seat Count, Missing counts for DSU, ICMT, SVDU, TPMU, Login user details not found, Please enter valid lruData","fleetData":{"airlineData":null,"dimAircraftJson":null,"configData":null}}
您得到 415 不受支持的媒体类型 因为您已经将 content-type header 覆盖为 application/x-www-form-urlencoded;字符集=UTF-8。只需将其更改为:
HttpPost httppost = new HttpPost('/createConfigData');
String jsondata = "{\"tailSign\": \"A7ALE\",\"active\": \"Y\"}";
StringEntity jsonparam = new StringEntity(jsondata);
jsonparam.setChunked(true);
httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.setEntity(jsonparam);
httpresponse = httpclient.execute(target, httppost);