java post request code example

Example: Post Request Example in Java

@BeforeAll
    public static void init(){
        RestAssured.baseURI = "http://........." ;
        RestAssured.port = 8000 ;
        RestAssured.basePath = "/api" ;
    }
String myBodyData = "{\n" +
                "  \"name\"  : \"Adam\",\n" +
                "  \"gender\": \"Male\",\n" +
                "  \"phone\": 6234567890\n" +
                "}" ;

        System.out.println("myBodyData = " + myBodyData);

        given()
                .contentType( ContentType.JSON )
                .body(myBodyData)
                .log().all().
        when()
                .post("/spartans").
        then()
                .log().all()
                .statusCode( is(201) )
                .contentType(ContentType.JSON)
                .body("success", is ("A Spartan is Born!"))
                .body("data.name", is ("Adam"))
        ;

Tags:

Java Example