Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Features of PUT Request
3.
Steps to Write a PUT Request
4.
Make The JSON Data Using the Simple JSON Library
5.
Send JSON Content in the Request’s Body
6.
Validate the Response
7.
Code
7.1.
Output
8.
Status Codes
9.
Frequently Asked Questions
9.1.
Does it return 200 or 201?
9.2.
Can we create using put?
9.3.
What should I return in PUT?
9.4.
What is the difference between PATCH, PUT and POST?
9.5.
Is put for an update or create?
10.
Conclusion
Last Updated: Mar 27, 2024
Medium

Let Us Write First PUT Request in REST Assured

Author Yashi Agarwal
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

The PUT method, aka HTTP PUT request method is used to pass data to a server to create or modify a resource. In other words, it changes a piece of resource at a specified URI. It is also used to make a new resource or replace the entire product entity at the given URI. The PUT request updates a resource and requires a full JSON payload. To send a PUT request in REST Assured, we use the put() method. We will discuss how to write a PUT request.

PUT Request in Rest Assured

Features of PUT Request

  • PUT requests are not cached.
  • PUT requests never remain in the browser history.
  • PUT requests cannot be bookmarked
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Steps to Write a PUT Request

  • Create JSON data using a simple JSON library.
  • Send the JSON content in the body of the Request.
  • Validate the response.

Let us discuss the steps below:

Make The JSON Data Using the Simple JSON Library

We will use a Public API to explain the PUT method in this article. Here we have all the Requests like PUT, PATCH, GET, and DELETE. In this article, our work is with PUT. You also make your local API for testing.

Send JSON Content in the Request’s Body

The Request is then sent to the server.

body(request.toJSONString()).
when().
put("https://reqres.in/api/users/2")

So we have created a request body as the JSON string, and then we have to call the "put()" method by sending the Reqress PUT link as an argument. This ensures that the record with the given Reqress data is completely updated.

Validate the Response

The next step is validating the response to ensure the record update. This is done by reading the particular status code of the response as follows:

then().
statusCode(201);

The steps discussed above are used to show the working of a PUT request and how it also differs from a POST request. We used the JSON library to create JSON data sent in the content of the body of the Request. We then have to validate the response using the status code after obtaining the response. I have successfully obtained the 201 status code.

Code

// Import all necessary classes
import org.testng.annotations.Test;
import org.json.simple.JSONObject;
import static io.restassured.RestAssured.*;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import org.codehaus.groovy.reflection.ReflectionCache;
public class Test_PUT {
    @Test
    void TEST_put() {
            // Create object of JSONObject type
            JSONObject request = new JSONObject();
            //passing data to be modified in put() as arguments
            request.put("name", "Aarya");
            request.put("job", "Developer");
            System.out.println(request);
            System.out.println(request.toJSONString());
            // Directly checking the status code of the given URL
            given().header("Content-Type", "application/json").contentType(ContentType.JSON).accept(ContentType.JSON).body(request.toJSONString()).
            when().
            put("https://reqres.in/api/users/2").
            then().
            statusCode(200);
    }
}

Output

output of put request

As we can see, our test code has passed successfully.

Status Codes

For a PUT request: HTTP 200, HTTP 204 should imply "resource updated successfully". HTTP 201 if the PUT request created a new resource. In the code shown above, We are getting a 201 status code which implies the put request has updated the data and means that the work has been done successfully.

Frequently Asked Questions

Does it return 200 or 201?

For any successful resource operations like modification (PUT), a 200 (OK) or 202 Accepted code is acceptable and returned. The 200 response code signifies that the modification was immediately completed.

Can we create using put?

No, PUT is not for an update or create. It is for replacement. You can not replace anything with something for the effect of making.

What should I return in PUT?

PUT should never return a body but must return a response code in the header. 

What is the difference between PATCH, PUT and POST?

POST is always for creating a resource (it does not matter if it was duplicated ), and PUT is for checking if the resource exists and then updating it. Else create a new resource. PATCH is always for updating a resource.

Is put for an update or create?

PUT creates and updates a resource's state on the server.

Conclusion

So basically, in this article, we have learned how to write a PUT request in Rest Assure. Three main steps are needed to be followed. I have mentioned an explanation for each point. To learn more about Rest API, check the link below:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Keep Coding!

Previous article
Let’s Write First POST Request in REST Assured
Next article
Let’s Write First PATCH Request in REST Assured
Live masterclass