In this post we’re going to
- send Http Request
- get Http Response
- get Http Response content as Java object
For sending requests and getting responses we will use Apache Http Client (org.apache.httpcomponents)
Apache Http Client Maven dependency
First of all we need to add a Maven dependency for Apache Http Client
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
Apache Http Client alows us to send Http requests and get Http responses.
Find other posts about GET, PUT and DELETE requests.
POST HTTP Request
This is how to create a simple POST HTTP request
HttpPost post = new HttpPost(url);
Here url
is the url of the web service endpoint or the url of the web site page.
Let’s assume we have some user service and we want to create a new user the url would be https://www.user-service-example.com/api/users
.
We should send a request with the new user info in JSON format.
HTTP Request Headers
To do that we have to set headers for the request. Headers is the array that contains key-value pairs. In our case headers look like this
Header headers[] = {
new BasicHeader("Content-type", "application/json"),
new BasicHeader("Accept", "application/json")
};
post.setHeaders(headers);
or like this
post.setHeader("Content-type", "application/json");
post.setHeader("Accept", "application/json"
That means that the request payload is in JSON format and we expect a response to be also in JSON format.
Now we should set the payload.
Gson gson = new Gson();
String json = gson.toJson(new User("John", "Doe", "john.doe@yahoogmail.com"));
post.setEntity(new StringEntity(json));
Apache HttpClient
Now you have to create a HTTP client, send POST request, get a response and close.
HttpClient client = HttpClients.custom().build();
HttpResponse response = client.execute(post);
HttpResponse. response.getEntity().getContent()
We access the Http response content with response.getEntity().getContent().
String result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
response.getEntity().getContent().close();
If you expect the response body (response.getEntity().getContent()) to be a JSON then you can convert that to a Java object.
Convert response.getEntity().getContent() from JSON to object
User user = gson.fromJson(result, User.class);
See more about How to convert JSON to Java Object.
Response Code
You also can verify the response code if you need.
int responseCode = response.getStatusLine().getStatusCode();
String statusPhrase = response.getStatusLine().getReasonPhrase();
Apache HttpClient - complete example
HttpPost post = new HttpPost(url);
Header headers[] = {
new BasicHeader("Content-type", "application/json"),
new BasicHeader("Accept", "application/json")
};
post.setHeaders(headers);
Gson gson = new Gson();
String json = gson.toJson(new User("John", "Doe", "john.doe@yahoogmail.com"));
post.setEntity(new StringEntity(json));
HttpClient client = HttpClients.custom().build();
HttpResponse response = client.execute(post);
String result = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);
User createdUser = gson.fromJson(result, User.class);
int responseCode = response.getStatusLine().getStatusCode();
String statusPhrase = response.getStatusLine().getReasonPhrase();
response.getEntity().getContent().close();
You may also find these posts interesting: