So far, we have covered sending a GET & POST Request in our tutorial on the REST Assured testing framework. If you haven’t checked that, let’s check ‘Sending GET Request’ by clicking this link and also check ‘Sending POST request’ using this link. Now, in this example, we are going to see “How to send an HTTP PUT request using Rest-Assured to a Rest API endpoint”.

Simple representation of PUT Request using RESTAssured...!!! Share on X

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

  1. What is the HTTP PUT Request?
  2. How to send PUT request using Rest Assured?
  3. How to validate the Response?

Let’s begin:

1. What is the HTTP PUT Request?

PUT method requests for the enclosed entity are stored under the Request-URI. An update operation will happen if the Request-URI refers to already existing resource otherwise there will be a create operation takes place if Request-URI is a valid resource URI.

Some key points of PUT requests:

  • PUT is idempotent means if you try to make a request multiple times, it would result in the same output as it would have no effect.
  • PUT should be used when you want to modify a resource which is already a part of resource collection as the PUT method would replace the resource entirely.
  • PUT resources can be cached.

2. How to send PUT request using Rest Assured?

In this tutorial, we will test the ‘Reqres.in Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purpose which is performing various CRUD operations.

Let’s take an example of one of the API PUT endpoint available at the above-mentioned website which is ‘/users/{id}’. The full-service URL with the endpoint is ‘http://reqres.in/api/users/{id}’.

At the above resource URL, we are going to submit data in the form of JSON to update an existing user which is having ‘id’ as ‘114’.

NOTE: This ‘id’ belongs to the user which is generated during the POST call to create the user. So, this id: 114 generated for me when I had executed the create user using the POST call. In order to get yours, first, execute the POST Request call available at this link and grab the ‘id‘ from the response and use it in your PUT Request Test below in place where I am using ‘114’. 

JSON Request Body:

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

Let’s try to understand the code:

1. Setting up Base URI

Base URI is the root address of the Resource. And, by this particular line of code, we are specifying to REST assured to use “reqres.in/api” as the root URL of the service.

2. Specifying the exact resource to look for and make a PUT request to that resource

Here, using this code, we are looking to make a PUT request to an exact resource which is “/users/114” in this case. Hence, the complete Service Endpoint would be “http://reqres.in/api/users/114” to which we are sending a PUT request with the JSON request body. The JSON request body is nothing but the details about the existing users that we are looking to update. The output of the PUT call will be stored in the REST Assured ‘Response’ object.

3. Response validation

a. response.asString() : It displays the response in a string format

b. response.getStatusCode() : This line of code would extract the status code from the response.

c. response.asString().contains(“Chris”) : This line of code helps to check if the string ‘Chris’ present in the response or not anymore. When the user gets added in the post call, it had the name ‘Chris’ but now we send the PUT request with the new name i.e ‘Adam’. Therefore, this validation should fail.

d. assertEquals(200, response.getStatusCode()) : This line of code will throw true or false based on the condition if status code from the response matches with the value 200 or not.

3. How to validate the Response?

I’ve already explained the validation above. Now, Let’s just execute the above Test class (PutRequest) in eclipse and verify the output:

a. Check for the Response i.e {“name”:”Adam”,”job”:”Java Developer”,”updatedAt”:”2022-04-21T08:42:51.102Z”}

b. Check for the Status Code i.e ‘200’ as intended.

c. Check if the ‘Chris’ string is present in the response i.e ‘false’ as intended because PUT request call changed the user name from ‘Chris’ to ‘Adam’.

d. Lastly, we performed assertEquals verification for Status Code, which is successfully passed, hence no failure in the eclipse output.

Console Output:

That’s it, it’s that simple to make a PUT Request with JSON body using REST Assured API: ?

Simple representation of REST Assured PUT Request example ...!!! Share on X

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

Other Useful References:

Author

  • Deepak Verma

    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.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak