Rest Assured Post - TestNG
package restAssuredTest;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItems;
import java.util.HashMap;
import java.util.Map;
import org.json.simple.JSONObject;
import org.testng.annotations.Test;
public class PostRequest { //in post request you have to pass the body
@Test
void postTest() {
// Map<String, Object> map = new HashMap<String, Object>();
// map.put("name", "John");//input parameter for post method
// map.put("job", "helper");//input parameter for post method
// System.out.println("The request is: "+map);
// //we need json library to show the result in json.
// //we can use gson dependency or jackson dependency or json dependency or json simple library for json
// //we are using json simple library
//
// JSONObject jsonRequest = new JSONObject(map);
// System.out.println("The json request is: "+jsonRequest);
// System.out.println("The json jsonRequest.toJSONString is: "+jsonRequest.toJSONString());//this shows same output like above line
//
//Above code can be written like below with out map
//---------------------------
JSONObject jsonRequest = new JSONObject();
jsonRequest.put("name", "John");//input parameter for post method
jsonRequest.put("job", "helper");//input parameter for post method
System.out.println("The json request is: "+jsonRequest);
System.out.println("The json jsonRequest.toJSONString is: "+jsonRequest.toJSONString());//this shows same output like above line
//------------------------------
given()
//.header("Key","Value");//if you need to give header, give here like this
//such as .header("Content-Type","application/json")
//.contentType(ContentType.JSON)
//.accept(ContentType.JSON)
.body(jsonRequest.toJSONString())
.when()
.post("https://reqres.in/api/users")
.then()
.statusCode(201);
}
}
When you test the URL https://reqres.in/api/users with below inputs, you get below data:
No comments:
Post a Comment