Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Preview HttpPost Request

Tags:

java

json

android

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.

like image 276
EZFrag Avatar asked Jan 23 '12 13:01

EZFrag


1 Answers

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.

like image 198
EZFrag Avatar answered Oct 05 '22 09:10

EZFrag