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.

Submitted by tgoswami on September 1, 2020

Tarun has 11+ years of experience in Quality Assurance in different domains like Banking, E-commerce, Health and Education.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.