Reputation: 6335
I am trying to post json object
{"venue":"places, US"}
using below code-
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 30000);
HttpResponse response;
try {
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
post.setHeader("Accept", "application/json");
post.setHeader("Content-type", "application/json");
response = client.execute(post);
} catch (Exception e) {
Log.e("Client", "sendJson", e);
}
However I am getting below error response from server-
{"description":"Invalid token character ',' in token \"json, application/json\"","errorCode":2000}
What is reason for that error from from server. Is is because of some header setting on server side or there is some problem with request code I pasted above.
Edit: Looks like this error is due to content type setting 2 times. After removing below line
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
error is gone.
But when I only remove below line instead of line written above, it does not remove error.
post.setHeader("Content-type", "application/json");
Why removing only first line works and when I remove 2nd one error is still there as both are setting content type.
Also I have used that code with both lines in it in other projects and it worked in them without any error. It looks like server side issue, what setting on server may have caused this problem.
Thanks
Upvotes: 1
Views: 1861
Reputation: 3394
It looks to me like you're setting the Content-Type
header twice. Once with the string literal at the end, and once at the beginning with the HTTP.CONTENT_TYPE
.
The way I solved this in my project was by extending StringEntity
to behave exactly the same, except for setting its Content-Type
to application/json
. That way, I never had to think about it again.
Upvotes: 2
Reputation: 15635
Try removing the
post.setHeader("Content-type", "application/json");
line. It looks like you'r adding the content-type header twice and the server has some problems with that.
Upvotes: 3