Java Send Http Post Example

Up until now, we have already covered sending a GET Request using Java 11 HttpClient API. If you haven't checked that, go for it by clicking this link. Now, in this example, we are going to see "How to send a POST request with JSON as request body using Java 11 HttpClient API?" .

Post_HttpClient_Request_Java11_Featured_Image_Techndeck

POST Request using Java 11 HttpClient API...!!! Click To Tweet

In this tutorial, we are going to cover below topics:

  1. What is HTTP POST Request?
  2. How to send POST request using HttpClient?

Check out: POST REQUEST using another popular API testing Framework – REST ASSURED

Let's begin:

1. What is HTTP POST Request?

POST is one of the most common methods of HTTP which is used to send data to a server to create/update the resource . Data sent to the server is in the form of either Request Body / Request Parameters which is basically used to create or update the resource on the server.

Some key points of POST requests:

  • POST requests parameters doesn't store in the browser history
  • POST requests are used to send data to the server to create or update the resource
  • POST requests cannot be cached
  • POST requests are secure as compared to GET because parameters/data doesn't store in browser history
  • POST requests parameter data is unlimited as there are no length restrictions
  • POST requests cannot be bookmarked
  • POST requests allow ASCII characters
  • POST requests are difficult to hack

2. How to send POST request using Java 11 HttpClient?

In this tutorial, we will test the 'Dummy Sample Rest API' which is available here. This page contains Fake Online REST API for the testing purposes which are performing various CRUD operations.

Let's take an example of one of the API POST endpoint available at the above-mentioned website which is '/create'. The full-service URL with endpoint is 'http://dummy.restapiexample.com/api/v1/create'.

At the above resource URL, we are going to submit data in the form of JSON to create an employee.

JSON Request Body:

Here is the code to send the POST request (containing request body in JSON format) to the above mentioned REST API Service Endpoint:

Let's try to understand the code:

1. Specify the URL

2. Create a Post Request using HttpRequest builder that takes JSON as input and pass the resource URI to it

3. Create a new HttpClient object

4. Submit the POST Request with BodyHandler which defines the response body should be of string format, and store the output in the response object

5. Finally, extract the status code and response body using the response object created above

Eclipse Console Output:

That's it, it's that simple to make a POST Request with JSON body using new Java 11 HttpClient library: ?

POST Request using Java 11 HttpClient API...!!! Click To Tweet

If you like this post , please check out my other useful blog posts on Rest Assured:

  • How to send a GET Request using Rest Assured
  • Configure Rest Assured in Eclipse

Other Useful References:

  • HttpClient 4 Tutorial – Baeldung
  • HttpClient Tutorial – Apache HC
  • Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

leemajew1957.blogspot.com

Source: https://techndeck.com/post-request-with-json-using-java-11-httpclient-api/

0 Response to "Java Send Http Post Example"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel