http://www.technicalpage.net/search/label/SQL

Rest Assured Post Method

 On this page, you can find java code for below Rest Assured Post Methods:

1. post with HashMap
2. Post without Hashmap 
3. post with Json String 
4. post with Json Object 


//import below packages:

import static io.restassured.RestAssured.*;

import static org.hamcrest.Matchers.*;

import java.util.*;

import org.json.JSONObject;


//1. post with HashMap

           public static void postWithHashmap() {

           HashMap<String, Object> data = new HashMap<>();

           data.put("title", "Post API test" );

           data.put("body", "Post API body" );

           data.put("userId", 2 );

           System.out.println("The hashmap data = "+data);

          

           given()

           //.contentType(ContentType.JSON)

           .header("Content-Type","application/json")

           //.contentType("application/json")

           .body(data)

           .when()

           .post("https://jsonplaceholder.typicode.com/posts")

           .then()

           .statusCode(201)

           .log().all()

           .body("title", equalTo("Post API test"))

           .body("body", equalTo("Post API body"))

           .body("userId", equalTo(2))

           ;

     }

Output:

The hashmap data = {title=Post API test, body=Post API body, userId=2}

HTTP/1.1 201 Created

Date: Sun, 07 Jun 2026 02:26:35 GMT

Content-Type: application/json; charset=utf-8

Content-Length: 85

Connection: keep-alive

access-control-allow-credentials: true

access-control-expose-headers: Location

Cache-Control: no-cache

etag: W/"55-mxvvF/CphGxL1Myq0eEKODlbcUk"

expires: -1

location: https://jsonplaceholder.typicode.com/posts/101

nel: {"report_to":"heroku-nel","response_headers":["Via"],"max_age":3600,"success_fraction":0.01,"failure_fraction":0.1}

pragma: no-cache

report-to: {"group":"heroku-nel","endpoints":[{"url":"https://nel.heroku.com/reports?s=jiTRtjHEr15VvELGFG5WFCmCCH6vduvmLRwAG2zcTGI%3D\u0026sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d\u0026ts=1780799194"}],"max_age":3600}

reporting-endpoints: heroku-nel="https://nel.heroku.com/reports?s=jiTRtjHEr15VvELGFG5WFCmCCH6vduvmLRwAG2zcTGI%3D&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&ts=1780799194"

Server: cloudflare

vary: Origin, X-HTTP-Method-Override, Accept-Encoding

via: 2.0 heroku-router

x-content-type-options: nosniff

x-powered-by: Express

x-ratelimit-limit: 1000

x-ratelimit-remaining: 999

x-ratelimit-reset: 1780799236

cf-cache-status: DYNAMIC

CF-RAY: a07c367829ad982d-DFW

alt-svc: h3=":443"; ma=86400

 

{

    "title": "Post API test",

    "body": "Post API body",

    "userId": 2,

    "id": 101

}

 

//2. Post without Hashmap

           public static void postWithoutHashMap() {

 

                      given()

                           .contentType("application/json")

                           .body("{\"title\":\"2nd test\",\"body\":\"2nd body\",\"userId\":3}")

                      .when()

                           .post("https://jsonplaceholder.typicode.com/posts")

                      .then()

                           .statusCode(201)

                           .log().all()

                           .body("title", equalTo("2nd test"))

                           .body("body", equalTo("2nd body"))

                           .body("userId", equalTo(3))

                           .body("id", equalTo(101))

                      ;

           }

Output:

HTTP/1.1 201 Created

Date: Sun, 07 Jun 2026 02:33:50 GMT

Content-Type: application/json; charset=utf-8

Content-Length: 75

Connection: keep-alive

Access-Control-Allow-Credentials: true

Access-Control-Expose-Headers: Location

Cache-Control: no-cache

Etag: W/"4b-xpzvhrEOOhPayD4v+boPAV+F5K8"

Expires: -1

Location: https://jsonplaceholder.typicode.com/posts/101

Nel: {"report_to":"heroku-nel","response_headers":["Via"],"max_age":3600,"success_fraction":0.01,"failure_fraction":0.1}

Pragma: no-cache

Report-To: {"group":"heroku-nel","endpoints":[{"url":"https://nel.heroku.com/reports?s=6n1sTmyWVoVgoYknYmjpyN0CNM5VCL3hX%2Beb8e20AtY%3D\u0026sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d\u0026ts=1780799630"}],"max_age":3600}

Reporting-Endpoints: heroku-nel="https://nel.heroku.com/reports?s=6n1sTmyWVoVgoYknYmjpyN0CNM5VCL3hX%2Beb8e20AtY%3D&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&ts=1780799630"

Server: cloudflare

Vary: Origin, X-HTTP-Method-Override, Accept-Encoding

Via: 2.0 heroku-router

X-Content-Type-Options: nosniff

X-Powered-By: Express

X-Ratelimit-Limit: 1000

X-Ratelimit-Remaining: 999

X-Ratelimit-Reset: 1780799656

Cf-Cache-Status: DYNAMIC

CF-RAY: a07c411b9b4f133e-DFW

alt-svc: h3=":443"; ma=86400

 

{

    "title": "2nd test",

    "body": "2nd body",

    "userId": 3,

    "id": 101

}

 

//3. post with Json String. This works with Java version 15+. If your java version is 15+ then you can give a try.

                      public static void postWithJsonObject() {

                      String data = """

                                 {

                                   "title": "3rd Post",

                                   "body": "3rd API body",

                                   "userId": 4

                                 }

                                 """;

                     

                      given()

                      //.header("Content-Type","application/json") OR

                      .contentType("application/json")

                      .body(data)

                      .when()

                     .post("https://jsonplaceholder.typicode.com/posts")

                      .then()

                      .statusCode(201)

                      .log().all()

                      .body("title", equalTo("3rd Post"))

                      .body("body", equalTo("3rd API body"))

                      .body("userId", equalTo(4))

                      ;

                }

 

4. post with Json Object

//import org.json.JSONObject; To use Json Object, we need this import

                      public static void postWithJsonObject() {

 

                           JSONObject data = new JSONObject();

                           data.put("title", "Post with JsonObject" );

                           data.put("body", "JsonObject body" );

                           data.put("userId", 4 );

                           System.out.println("The JsonObject data = "+data);

                          

                     

                      given()

                      .contentType("application/json")

                      .body(data.toString())

                      .when()

                     .post("https://jsonplaceholder.typicode.com/posts")

                      .then()

                      .statusCode(201)

                      .log().all()

                      .body("title", equalTo("Post with JsonObject" ))

                      .body("body", equalTo("JsonObject body"))

                      .body("userId", equalTo(4))

                      .body("id", equalTo(101))

                      ;

                }

 

Output:

The JsonObject data = {"title":"Post with JsonObject","body":"JsonObject body","userId":4}

HTTP/1.1 201 Created

Date: Sun, 07 Jun 2026 02:57:22 GMT

Content-Type: application/json; charset=utf-8

Content-Length: 94

Connection: keep-alive

access-control-allow-credentials: true

access-control-expose-headers: Location

Cache-Control: no-cache

etag: W/"5e-zQQCsHqyfvq+QZ8GiukV9pQw7JU"

expires: -1

location: https://jsonplaceholder.typicode.com/posts/101

nel: {"report_to":"heroku-nel","response_headers":["Via"],"max_age":3600,"success_fraction":0.01,"failure_fraction":0.1}

pragma: no-cache

report-to: {"group":"heroku-nel","endpoints":[{"url":"https://nel.heroku.com/reports?s=fKprskskjzOnz3qPNAdmgSy4y2qudeGuNH5Xy3snDkc%3D\u0026sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d\u0026ts=1780801042"}],"max_age":3600}

reporting-endpoints: heroku-nel="https://nel.heroku.com/reports?s=fKprskskjzOnz3qPNAdmgSy4y2qudeGuNH5Xy3snDkc%3D&sid=e11707d5-02a7-43ef-b45e-2cf4d2036f7d&ts=1780801042"

Server: cloudflare

vary: Origin, X-HTTP-Method-Override, Accept-Encoding

via: 2.0 heroku-router

x-content-type-options: nosniff

x-powered-by: Express

x-ratelimit-limit: 1000

x-ratelimit-remaining: 999

x-ratelimit-reset: 1780801096

cf-cache-status: DYNAMIC

CF-RAY: a07c63952a453ea2-DFW

alt-svc: h3=":443"; ma=86400

 

{

    "title": "Post with JsonObject",

    "body": "JsonObject body",

    "userId": 4,

    "id": 101

}

 

No comments:

Post a Comment