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"
}

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.