How to send POST request using Rest Assured framework in Java ?

Author: neptune | 26th-Mar-2023
#Selenium #API

Rest Assured is a popular Java-based framework that is used for testing RESTful web services. It provides a simple and intuitive API for sending HTTP requests and validating the responses. In this article, we will discuss how to send a POST request using Rest Assured framework in Java with examples.


Before we dive into the details, let's first understand what a POST request is and why it is used.



What is a POST request?

A POST request is a type of HTTP request method used to submit data to a server. It is commonly used to create or update a resource on the server. When a client sends a POST request to a server, the server processes the request and sends back a response.



Why use a POST request?

A POST request is used when we want to submit data to a server. For example, when we fill out a form on a website and click the submit button, a POST request is sent to the server with the data we entered in the form. Similarly, when we create a new user account on a website, a POST request is sent to the server with the user's details.


Now that we have a basic understanding of what a POST request is and why it is used, let's move on to how to send a POST request using Rest Assured framework in Java.



Step 1: Add Rest Assured dependency to your project

The first step is to add the Rest Assured dependency to your project. You can do this by adding the following dependency to your pom.xml file if you are using Maven.


```

<dependency>

    <groupId>io.rest-assured</groupId>

    <artifactId>rest-assured</artifactId>

    <version>4.3.3</version>

    <scope>test</scope>

</dependency>

```


If you are using Gradle, you can add the following dependency to your build.gradle file.


```

testImplementation 'io.rest-assured:rest-assured:4.3.3'

```



Step 2: Create a POST request

The next step is to create a POST request using Rest Assured. You can do this by using the given() method to specify the base URL of the API endpoint and the body() method to specify the data you want to send in the request.


For example, let's say we want to create a new user account on a website. We can send a POST request to the server with the user's details in the request body. Here's how we can create a POST request using Rest Assured.


```

given()

    .baseUri("https://example.com/api")

    .body("{ \"name\": \"John Doe\", \"email\": \"[email protected]\", \"password\": \"password123\" }")

.when()

    .post("/users")

.then()

    .statusCode(201);

```


In the above example, we are sending a POST request to the /users endpoint with the user's details in the request body. We are using the given() method to specify the base URL of the API endpoint and the body() method to specify the data we want to send in the request.



Step 3: Validate the response

The final step is to validate the response we receive from the server. We can do this by using the then() method to specify the expected status code and any other assertions we want to make on the response.


For example, let's say we expect the server to return a status code of 201 (Created) if the user account is successfully created. We can validate the response using the following code.


```

.then()

    .statusCode(201);

```


In the above example, we are using the then() method to specify that we expect the server to return a status code of 201 (Created) if the user account is successfully created.



Complete example

Here's a complete example of how to send a POST request using Rest Assured framework in Java.


```

import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.*;

import static org.hamcrest.Matchers.*;


public class CreateUserTest {


    @Test

    public void testCreateUser() {

        given()

            .baseUri("https://example.com/api")

            .body("{ \"name\": \"John Doe\", \"email\": \"[email protected]\", \"password\": \"password123\" }")

        .when()

            .post("users")

        .then()

            .statusCode(201)

            .body("name", equalTo("John Doe"))

            .body("email", equalTo("[email protected]"));

    }

}

```



In the above example, we are sending a POST request to the /users endpoint with the user's details in the request body. We are using the given() method to specify the base URL of the API endpoint and the body() method to specify the data we want to send in the request.


We are then using the when() method to send the request and the then() method to validate the response. We are specifying that we expect the server to return a status code of 201 (Created) if the user account is successfully created. We are also using the body() method to make assertions on the response body.



Conclusion

In this article, we discussed how to send a POST request using Rest Assured framework in Java with examples. Rest Assured provides a simple and intuitive API for sending HTTP requests and validating the responses. By following the steps outlined in this article, you can easily create and send a POST request using Rest Assured in your Java project.





Related Blogs
Automate Ticket Booking in SpiceJet from Delhi to Bengaluru using Selenium and Cucumber.
Author: neptune | 08th-Aug-2023
#Selenium
We are going to automate a ticket booking using Selenium WebDriver and Cucumber BDD...

Selenium, Cucumber, JUnit, TestNG dependencies for Selenium project.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
We guide you how to update the pom.xml file for Selenium Maven project...

5 Selenium Project Ideas & for Beginners in Automation Testing
Author: neptune | 30th-Mar-2023
#Selenium #Testing #Projects
In this article, we will discuss 5 interesting Selenium project ideas for beginners in automation testing...

How to use wait commands in Selenium WebDriver in Java ?
Author: neptune | 22nd-Feb-2022
#Selenium #Testing #Java
We are going to explore different types of waits in Selenium WebDriver. Implicit wait, Explicit wait, and Fluent wait with examples...

Top 50+ Selenium Interviews Questions 2023 based on Years of Experience
Author: neptune | 02nd-Apr-2023
#Selenium #Testing #Interview
Every interview difficulty is based on how many years of experience you have in that field. For the Selenium Automation Tester I have divided the question on the number of years of experience...

Architecture of API Gateway
Author: neptune | 15th-Nov-2022
#API #Microservice
Creating an API starts with the publisher, where it will be designed, and it will be published to the store for the consumer to explore and subscribe...

Top 10 Selenium Interview Questions with answers (2021).
Author: neptune | 02nd-Apr-2023
#Selenium #Interview
In this article I will cover top 10 Selenium interview questions...

How to create Selenium Cucumber Project in Eclipse.
Author: neptune | 25th-May-2022
#Selenium
First, divide the process into the various steps to understand working in brief of a project. Steps in brief: We’ll start with initializing the browser driver and then log in to the web page...

Challenges faced in automating Web Applications using Selenium
Author: neptune | 02nd-Oct-2022
#Selenium #Testing
List of Challenges faced by testers using Selenium 1. Popup and Alert Handling, 2. Dynamic element Handling 3. Restricted to only Desktop Browsers Testing...

How to create a Selenium, Cucumber Automation project in Eclipse ?
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
Problem Statement : Let’s consider this particular project where we will try automating the process of booking a flight ticket. Let’s get started and see how it’s done using Selenium...

20 TestNG Interview Questions
Author: neptune | 17th-Dec-2022
#Selenium #Interview
There is always more than one test or method in the class. If we do not prioritize these tests or methods, then the methods are selected alphabetically and executed while execution...

Getting started with Selenium WebDriver.
Author: neptune | 02nd-Apr-2023
#Selenium #Testing
In this blog I will give you an overview of Selenium WebDriver and Also discuss about advantages and disadvantages of Selenium WebDriver...

Why we use Robot Class in Automation Testing?
Author: neptune | 23rd-Aug-2022
#Selenium
Robot class is used to generate native system input events for the purpose of automation testing. It is used to perform mouse and keyboard events on windows applications or popups...

Top 10 Selenium Interview Questions.
Author: neptune | 02nd-Apr-2023
#Selenium #Interview
Locator is a command that tells Selenium IDE which GUI elements (like Text Box, Buttons, Check Boxes etc) we are going to use or perform some automation task...

View More