1
votes

How to use post method in rest assured framework. I am learning API by using Rest Assured Framework, can anyone help me how to construct

My code:

    RestAssured.baseURI ="https://reqres.in";
    RequestSpecification request = RestAssured.given();

    JSONObject requestParams = new JSONObject();        
    requestParams.put("email",  "sydney@fife");
    requestParams.put("password", "pistol444");
    request.body(requestParams.toJSONString());
    Response response = request.post("/api/register");

    int statusCode = response.getStatusCode();
    System.out.println("API response code is :" + statusCode);
    String body =response.asString();
    System.out.println("Response body is : "+ body);

Getting error message after consume this Post method :

API response code is :400 Response body is : {"error":"Missing email or username"}

This is reference URL: https://reqres.in/ and post method is : REGISTER - SUCCESSFUL

Please help me .

Thanks

1

1 Answers

2
votes

You have not added the header, third line in the code below

RestAssured.baseURI = "https://reqres.in";

RequestSpecification request = RestAssured.given();
request.header("Content-Type","application/json");

JSONObject requestParams = new JSONObject();