Kayvar Kayvar - 1 month ago 43
Apache Configuration Question

How to send a POST with JSON parameters using Apache HTTPClient 4.5. Getting 415 unsupported media type

I'm trying to send a POST request to my server (using Apache HTTPClient 4.5) with a set list of JSON parameters. I've followed some SO questions, but am running into problems.

When I use a javascript console to send the request, it works! Like this:

//Using JS console, I send a POST request and it works.
$.post('/createConfigData', {
"tailSign": "A7ALE",
"active": "Y"
});
//Get back 201


When I use Apache HTTPClient 4.5 to try to do the same thing as above, I get back 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);


The data I have from the working request is:


  • Request Headers:


    • Host: "mysite.com"

    • User-Agent: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0"

    • Accept:
      "*/*"

    • Accept-Language: "en-US,en;q=0.5"

    • Accept-Encoding: "gzip, deflate"

    • Content-Type: "application/x-www-form-urlencoded; charset=UTF-8"

    • Referer: "mysite.com/index.html"

    • Content-Length: "288"

    • Cookie: "JSESSIONID=..."

    • Proxy-Authorization: "Basic ..."

    • Connection: "keep-alive"

    • Pragma: "no-cache"

    • Cache-Control: "no-cache"


  • Response Headers:


    • Age: "0"

    • Connection: "Keep-Alive"

    • Content-Type: "application/json;charset=UTF-8"

    • Date: "Wed, 19 Oct 2016 17:57:34 GMT"

    • Server: "Apache-Coyote/1.1"

    • Transfer-Encoding: "chunked"




I'm a bit confused on where to set the content type, whether it be the entity or the httppost

=======================

More test cases



Setting httpost's content type to json gives me
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);





Setting httpost's content type to x-www-form-urlencoded gives me
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);





I also tried adding this line:

httppost.addHeader("Accept", "*/*");





==================

SOLUTION



Using wireshark I was able to figure out that there was an error message that goes along with the 400 bad request! It was just my JSON wasn't correct.

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}}

Answer

You are getting 415 unsupported media type since your has overwritten content-type header to application/x-www-form-urlencoded; charset=UTF-8. Just change it to:

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);