Skip to main content

PUT Request using REST Assured

The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload. 

If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

If a new resource is created, the origin server MUST inform the user agent via the 201 (Created) response. If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request.

public class ReqRes 
{
	public static String id = "2";

	@BeforeClass
	public void setup()
	{
		RestAssured.baseURI = "https://reqres.in/";
		RestAssured.basePath = "api";
	}

	/*Put request example*/
	@Test(enabled=true)
	public void putExample()
	{
		Response res = 
				given()
					.header("Content-Type","application/json")
					.body("{\n" + 
							"    \"name\": \"Tarun Goswami1\",\n" + 
							"    \"job\": \"QA\"\n" + 
							"}")
				.when()
					.put("/users")
				.then()
					.statusCode(200)
					.extract().response();
		
		System.out.println(res.body().prettyPrint());
	}
}

In above code if the request is successful it will return 200.

Video Tutorial

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.