How to send PUT Request in Rest Assured? (Updated 2020)

This entry is part 5 of 14 in the series Rest Assured Tutorial

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...!!! Click To Tweet

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 ‘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 PUT endpoint available at the above-mentioned website which is ‘/update/{id}’. The full-service URL with the endpoint is ‘http://dummy.restapiexample.com/api/v1/update/{id}’.

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

NOTE: This ‘id’ belongs to the employee which is generated during the POST call to create the employee. So, this id: 4710 generated for me when I had executed the create employee 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 ‘4710’. 

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 “dummy.restapiexample.com/api/v1” 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 “/update/4710” in this case. Hence, the complete Service Endpoint would be “http://dummy.restapiexample.com/api/v1/update/4710” 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 employee 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(“tammy”) : This line of code helps to check if the string ‘tammy’ present in the response or not anymore. When the employee gets added it had the name ‘tammy’ but now we send the PUT request with the new name i.e ‘put_test_employee’. 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”:”put_test_employee”,”salary”:”1123″,”age”:”23″}

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

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

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

Eclipse 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 ...!!! Click To Tweet

 

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

Other Useful References:

 

Related Posts:

[wcp-carousel id=”5342″]
Series Navigation<< How to send a POST Request using Rest Assured [2020]How to send DELETE request using REST Assured(Updated) >>

Leave a Reply

Your email address will not be published.Required fields are marked *

Facebook Twitter Instagram YouTube