coding made simple with some delicious cake

Java HttpClient PUT request example

In this tutorial we will continue providing examples of how to use Apache HttpClient, in this post we will concentrate on how to make a HttpClient PUT request. Please also take time to visit our other recent posts relating to how to make a GET and POST request using HttpClient.

Setup

In order to use the HttpClient you will need to include the following dependancy

Maven dependency

1

2

3

4

5

<dependency>

<groupId>org.apache.httpcomponents</groupId>

<artifactId>httpclient</artifactId>

<version>4.5.4</version>

</dependency>

HttpClient PUT request example

In the following example (very similar to our other recent posts) we are using the HttpClient to make a HTTP PUT request, it shows how to:

Output

Running the above code sample produces the following output

Output

1

2

3

4

5

6

7

statusCode = 200

content = {

"userId": "user1",

"id": 1,

"title": "new-post-title",

"body": "new-post-body"

}

In conclusion I hope this post has given you a basic understanding of how to make a HTTP PUT request using the apache HttpClient library. Please also visit our other post on how to make other HTTP requests with apache HttpClient.