Pradeep,
Below is the sample code to pass the request body, If you need more help on http-client API , please go through the apache API documentation (Apache HttpClient 4.3.6 API).
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
HttpPatch patch = new HttpPatch("http://localhost:6001/addBooks");
patch.setHeader("Content-Type", "application/json");
String body = "{"+
" \"book\": ["+
" "+
" {"+
" \"id\":\"01\","+
" \"language\": \"Java\","+
" \"edition\": \"third\","+
" \"author\": \"Herbert Schildt\""+
" },"+
" "+
" {"+
" \"id\":\"07\","+
" \"language\": \"C++\","+
" \"edition\": \"second\","+
" \"author\": \"E.Balagurusamy\""+
" }"+
" ]"+
"}";
StringEntity entity = new StringEntity(body);
patch.setEntity(entity);
CloseableHttpClient httpclient = HttpClients.createDefault();
response = httpclient.execute(patch);
HttpEntity entityResponse = response.getEntity();
if (entityResponse != null) {
long len = entityResponse.getContentLength();
if (len != -1 && len < 2048) {
builder.append(EntityUtils.toString(entityResponse));
} else {
return "Invalid Content Length";
}
}
} catch (ClientProtocolException e) {
builder.append(e.getMessage());
builder.append(e.getStackTrace());
} catch (IOException e) {
builder.append(e.getMessage());
builder.append(e.getStackTrace());
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
builder.append(e.getMessage());
builder.append(e.getStackTrace());
}
}
}
return builder.toString();
}
Thanks,
Vamsi