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 | 06th-Jul-2024
#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...

How I Built My Blogging Website Using React, Node.js, and Jamstack Architecture?
Author: neptune | 31st-Jul-2024
#JavaScript #API
Building a blogging website using React, Node.js, and Jamstack architecture was a rewarding experience...

Why API Authentication?
Author: neptune | 01st-Jan-2023
#API
API's play a vital role in linking applications to exchange services and data. There are a variety of ways to authenticate API requests...

Why, What, and When: Understanding Jamstack?
Author: neptune | 05th-Jul-2024
#JavaScript #API
Jamstack represents a modern approach to web development that addresses many of the challenges faced by traditional architectures...

How to Get Started with Jamstack: A Comprehensive Guide?
Author: neptune | 05th-Jul-2024
#JavaScript #API
Getting started with Jamstack involves choosing the right tools, setting up a structured development environment...

View More