Skip to main content

Post Request using REST Assured

A POST request is used to send data to the server, for example, customer information, file upload, etc. using HTML forms.

public class ReqRes 
{

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

	
	/*POST Request Example*/
	@Test(enabled=true)
	public void postRequestExam()
	{
		Response res = 
		given()
			.header("Content-Type", "application/json")			
			.body("{\n" + 
					"    \"name\": \"Tarun Goswami\",\n" + 
					"    \"job\": \"QA\"\n" + 
					"}")
			  
		.when()
			.post("/users")
		.then()
			.statusCode(201)
			.extract().response();
		
		System.out.println(res.body().prettyPrint());
	}
}

Output

{
    "name": "Tarun Goswami",
    "job": "QA",
    "id": "591",
    "createdAt": "2020-07-24T07:38:08.739Z"
}
Video Tutorial

About

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