kushi
kushi

Reputation: 77

Request body in post method java

I am using Apache Commons HttpClient Post method. In post method there are two ways to set request body.

1) setRequestBody(NameValuePair [])

2) setRequestEntity(RequestEntity)

In my case, the input for the above methods would be json Object. How can i send json Object as requestBody ?

Upvotes: 2

Views: 9074

Answers (1)

Aleks G
Aleks G

Reputation: 57306

You need to use the second option:

httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

String json = "{ \"key\" : \"value\" }";
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);

Upvotes: 3

Related Questions