Explain PUT request in Rest Assured.


A PUT request is used to pass data to the server for the creation or modification of a resource. The difference between POST and PUT is that POST request is not idempotent.

This means invoking the same PUT request numerous times will always yield the same output. But invoking the same POST request numerous times will create a similar resource more than one time.

The status codes for PUT requests are −

  • 200 - request is successful along with modification in the Response body.
  • 400 - request is unsuccessful.
  • 204 - request is successful without content.

Example

Code Implementation

import org.testng.annotations.Test;
import static io.restassured.RestAssured.*;
import io.restassured.RestAssured;
public class NewTest {
   @Test
   void test() {

      //update title in body
      String b = "{
" + " \"title\": \"Tutorialspoint\"}";       //base URL       RestAssured.baseURI = "https://jsonplaceholder.typicode.com";       //update title in body for resource id = 1       given().header("Content-type", "application/json")       //adding put method       .body(b).when().put("/posts/1").then().log().all()       //verify status code as 200       .assertThat().statusCode(200);    } }


Updated on: 18-Nov-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements