It’s not working…POST request using Rest Assured

import org.json.simple.JSONObject;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class SimplePostTest {
RequestSpecification httpRequest;
Response response;

@BeforeClass
public void Setup()
{
RestAssured.baseURI = “http://restapi.demoqa.com/customer”;
httpRequest = RestAssured.given();
JSONObject requestParams = new JSONObject();

requestParams.put(“FirstName”, “Poonam”); // Cast
requestParams.put(“LastName”, “Agrawal”);

requestParams.put(“UserName”, “simpleuser001”);
requestParams.put(“Password”, “password1”);
requestParams.put(“Email”, “someuser@gmail.com”);

httpRequest.header(“Content-Type”,”application/json”);
// Add the Json to the body of the request
httpRequest.body(requestParams.toJSONString());
// Post the request and check the response
response = httpRequest.post(“/register”);

}

@Test
public void ResponseValidationTest()
{
int StatusCode = response.getStatusCode();
String statusLine = response.getStatusLine();
String successCode = response.jsonPath().get(“SuccessCode”);

System.out.println(StatusCode);
System.out.println(statusLine);
System.out.println(successCode);
//Assert.assertEquals(StatusCode, “201”);

}

@AfterClass
public void cleanUp()
{
//httpRequest = null;
//response = null;
}
}

Default Asked on June 27, 2018 in Program QA Automation.
Add Comment
  • 1 Answer(s)

    If you specify what is output you are facing then it will be easy to check it. However I have used your code and it works fine. I can see it working.

    The code is used as it is and below is output. Just changed input values to create user.

    RE: It's not working...POST request using Rest Assured

    Default Answered on July 6, 2018.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.