Toran Billups
Toran Billups

Reputation: 27399

How to add json to the body of an http post in java

I'm trying to post some JSON data in java for an Android app I'm working on. Is the below valid or do I need to push the JSON string in a different way?

HttpPost httpost = new HttpPost("http://test.localhost");
httpost.setEntity(new StringEntity("{\"filters\":true}"));
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
//... other java code to execute the apache httpclient

Thank you in advance

Upvotes: 40

Views: 36807

Answers (1)

laz
laz

Reputation: 28638

You should set the Content-Type header to "application/json". Everything else looks good.

Upvotes: 41

Related Questions