I am trying to send JSON formatted data to a server using Java. The information is getting to the server, but the server is responding with a "Bad Request".
HttpPost httpost = new HttpPost(path);
StringEntity se = new StringEntity(JSONRequest);
//sets the post request as the resulting string
httpost.setEntity(se);
//sets a request header so the page receving the request will know what to do with it
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json;charset=utf8");
HttpResponse response = httpclient.execute(httpost);
That is the basic setup of my request. Here is the JSONData:
{"clientApplicationDto":{"AuthenticationToken":"","BrandId":12,"MobileDeviceApplicationId":0},"mobileDeviceInfo":{"CarrierName":"MTN-SA","OsVersion":"2.2.2","ClientApplicationVersion":"TEST","DeviceManufacturer":"HTC","DeviceName":"HTC Desire","DeviceUniqueId":"1e9766fa2ef4c53a","OsName":"8","ClientApplicationTypeId":3}}
If this looks right to you lot, I'll start spamming the admins, but for now, I need to know if I am missing something.
I found the issue... The server is extremely sensitive to the content type header and the content format
httpost.setHeader("Content-type", "application/json;charset=utf8");
Needed to be changed to
httpost.setHeader("Content-type", "application/json; charset=utf-8");
and StringEntity se = new StringEntity(JSONRequest);
needed to be changed to
StringEntity se = new StringEntity(JSONRequest,"utf-8");
Thanks Jens, that one comment pushed me into the right direction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With