Table of contents
1.
Introduction
2.
REST Assured
3.
POST Request Method using Rest Assured
4.
Set Up Project
5.
Writing First POST Request in REST Assured
5.1.
Explanation
6.
Frequently Asked Questions
6.1.
What is the purpose of POST?
6.2.
Can we include parameters in our POST request?
6.3.
Why do we use REST assured?
6.4.
Has Rest assured a useful tool?
6.5.
What is REST API testing?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

Let’s Write First POST Request in REST Assured

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hello Ninjas! Today we will discuss Rest assured and the First POST Request in REST Assured.

Let’s Write First POST Request in REST Assured

Rest assured is a library in Java that allows you to test RESTful APIs. It's famous for testing JSON and XML-based web applications. Furthermore, it supports all REST methods, such as GET, PUT, POST, PATCH, and DELETE.

Talking more about Rest assured, let's move towards what is REST assured.

REST Assured

Rest Assured integrates well with Maven and allows you to test REST APIs using Java libraries. It employs very efficient matching techniques, making it simple to assert your expected results. 

REST Assured
  • Rest assured, no matter how complex the JSON structures are, Rest Assured has methods to retrieve data from almost every part of the request and response.
     
  • API Automation Testing is still relatively new and specializes in the testing community. The complexities of JSON prevent API testing from being explored. 
     
  • However, this does not diminish its significance in the testing process. You can rest assured. Learning is desirable because the io framework has made it very simple using core java basics.
     

Let's move to our main topic, "Let's Write First POST Request in REST Assured."

POST Request Method using Rest Assured

Rest Assured uses the post() method to send an HTTP POST request.

The steps for making a POST Request with Rest Assured are as follows.

  • Make a Request that points to the service Endpoint.
     
  • Make a JSON Request that includes all of the fields.
     
  • Add a JSON body to the request and send it.
     
  • Verify the Request.
     
  • Modifying the HTTP Method on a POST Request


To use REST assured in your project, you must include the dependencies listed below.

<dependencies>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured-common -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured-common</artifactId>
    <version>5.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured-all -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured-all</artifactId>
    <version>5.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.4.0</version>
    <scope>test</scope>
</dependency>

</dependencies>

Set Up Project

Let us now set up our project. Follow the following steps.

1️⃣ Download Eclipse.
 

2️⃣ Now select "Eclipse IDE for Java Developers" and complete the Rest of the installation process.

Eclipse IDE for Java Developers

3️⃣ After installation, start "Eclipse IDE", select a directory as a workspace and click on "Launch."

Eclipse IDE

4️⃣ Go to File > New > Project to create a “New Project”.

new project

5️⃣ Select the “Maven Project” and click on “Next”. 

maven project

6️⃣ Create a simple project and click on “Next”.

simple maven project

7️⃣ Fill in “Group Id:” and “Artifact Id:” and click on “Finish”.

filling the fields in new maven project

8️⃣ Now go to Project Explorer, click the pom.xml fileedit as shown below, and save it.

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>POSTRequestRESTAssured</groupId>
  <artifactId>kanak</artifactId>
  <version>0.0.1-SNAPSHOT</version>
 
<dependencies>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.2.0</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured-common -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured-common</artifactId>
    <version>5.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured-all -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured-all</artifactId>
    <version>5.2.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.4.0</version>
    <scope>test</scope>
</dependency>

</dependencies>

</project>


9️⃣ Write the first POST request in REST Assured test. We need to add a TestNG plug-in. To add the plug-in, follow the following steps.

  1. In the navigation bar, click "Help" and select "Install New Software…".
step 1

2. Paste the URL "https://testng.org/testng-eclipse-update-site" in the “Work with:” text field, select the plug-ins, and click "Next."

Step 2

3. Click on "Finish".

step 3

4. Tick the "Unsigned" and "Always trust all content" checkboxes, then click on "Trust Selected" and restart Eclipse.

step 4

Alright! We are done with setting up the project. Now let us write first POST Request in Rest Assured.

Writing First POST Request in REST Assured

1️⃣ Select the "src/test/java" folder > right-click on it and go to New > Package to create a Java package.

step 1

2️⃣ Give a package name (say javaPackageName) and click on "Finish."

step 2

3️⃣ Right-click on the “javaPackageName” and go to New > Class to create a Java class.

step 3

4️⃣ Input the Java class name(say POSTRequest) and click on "Finish".

step 4

5️⃣ Now edit the “POSTRequest.java” file as shown below and save it.

POSTRequest.java:

package javaPackageName;

import org.testng.annotations.Test;
import io.restassured.http.ContentType;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;


public class POSTRequest {
    @Test
   
    public void post_request()
    {
       
   
    String jsonString = "{\"name\" : \"kanak\",\"password\" : \"kanak123\"}";
   
    /*
    	Construct a request specification
    */
    RequestSpecification request= RestAssured.given();
   
    /*
    	Setting content type to specify the format in which the request payload will be sent.
    */
    request.contentType(ContentType.JSON);

    /*
    	Adding URI
    */
    request.baseUri("https://reqres.in/api/users");

    /*
    	Adding body as string
    */
    request.body(jsonString);
   
    /*
    	Calling POST method on URI. After hitting, we get Response
    */
    Response response = request.post();
   
    /*
    	Printing Response as string
    */
    System.out.println(response.asString());
   
    /*
    	Get Validatable response to perform validation
    */
    ValidatableResponse validatableResponse = response.then();
   
    /*
    	Validate status code as 201
    */
    validatableResponse.statusCode(201);

    System.out.println(response.getStatusCode());
    System.out.println(response.getTime());
    }
}
You can also try this code with Online Java Compiler
Run Code


6️⃣ Now in the navigation bar click on “Run” and go to “Run As” > “1 TestNG Test”.

step 6

7️⃣ Now go to “Console,” and you will see output similar to as shown below.

Step 7

Explanation

In the above code, we created a method called "post_request". Under this method, we defined a JSON string variable of string type to store JSON values. Then we constructed a request specification, set the content type, and added URI. After we added URI, we added the body as a string. Then we called the POST method on URI, printed the Response, and performed validation.

Frequently Asked Questions

What is the purpose of POST?

POST is a method of sending data to a server to create or update a resource. A few words about POST requests: POST requests never get cached. POST requests are not saved in the browser's history.

Can we include parameters in our POST request?

The parameters are sent as the body of the request after the headers in a POST request. To perform a POST with HttpURLConnection, you must first open the connection and then write its parameters.

Why do we use REST assured?

It eliminates the need to write a large amount of source code to establish an HTTP connection, send a request, and receive and parse a response.

Has Rest assured a useful tool?

REST Assured is not a stand-alone application. It is a library written in Java. To use it, you must create a new Java project and include it as a library in your project. Of course, many build tools are supported, so you can simply point your dependency resolver to a public (Maven) database.

What is REST API testing?

REST API testing is an open-source technique for testing RESTful APIs for web applications. It is commonly used to test JSON and XML-based web applications. It works with all methods, including GET, PUT, POST, PATCH, and DELETE. The REST API is a Java library.

Conclusion

In this blog, we have learned how to write first POST REST assured test. The articles contain REST Assured how to use a POST request. The steps to set up Rest Assured are First POST Request in REST Assured test.

If you found this blog has helped you enhance your knowledge, and if you want to learn articles like First POST Request in REST Assured test, check out our articles below:

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass