How to update the value of a field in a request using Rest Assured?


We can update the value of a field in a request using Rest Assured. This can be achieved with the help of the PUT request. A PUT request is used to pass data to the server for the 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.

Let us send a GET request on an API and observe its response using Postman.

Using Rest Assured, we shall update the value of the title field from sunt aut facere repellat provident occaecati excepturi optio reprehenderit to Tutorialspoint.

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);    } }

Output

Updated on: 17-Nov-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements