Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

REST Assured Tutorial 9 – Let’s Write First POST Request in REST Assured

Posted on 01/14/2025 By admin

As a part of End to End REST Assured Tutorial , in this post We will write first POST request in REST assured.

I will strongly recommended you to learn basics of API testing from here.

To learn what is POST request and how can you hit a POST request in Postman tool, must refer this post.

We will automate a POST request named “GetAuth” from Restful Booker. We will perform some validations like status code as well.

GetAuth API takes body as below where you need to pass username and password. If you pass credentials correctly, you will get token else error message.

 
{
    "username" : "admin",
    "password" : "password123"
}
 

Let’s start writing a POST request in REST Assured using both BDD and Non-BDD style. We will see multiple ways of writing same test as well.

We know that we need to pass a payload ( JSON or XML) to POST request. REST Assured makes it very handy to pass body to request. We can pass body as a String or a JSON file or a XML file or a Java Object or a byte array. We will see all ways in upcoming posts but as of now we will pass body as a String.

package RestfulBooker.postExamples;

import org.hamcrest.Matchers;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;

public class NonBDDStylePostRequest {
        
        
        public static void main(String[] args) {
                
                // There is no need to add escape character manually. Just paste string within double 
                // quotes. It will automatically add escape sequence as required. 
                String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}";
                
                // Create a request specification 
                RequestSpecification request= RestAssured.given();
                
                // Setting content type to specify format in which request payload will be sent.
                // ContentType is an ENUM. 
                request.contentType(ContentType.JSON);
                //Adding URI
                request.baseUri("https://restful-booker.herokuapp.com/auth");
                // 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 200
                validatableResponse.statusCode(200);
                
                // Validate token field is null
                // SInce response is one to one mapping so passing key name will give you value.
                // Below method validates that value of token is not null.
                validatableResponse.body("token", Matchers.notNullValue());
                
                // Validate token length is 15
                validatableResponse.body("token.length()", Matchers.is(15));
                
                // Validate token is an alphanumeric value
                validatableResponse.body("token", Matchers.matchesRegex("^[a-z0-9]+$"));
        }

}

package RestfulBooker.postExamples;

import org.hamcrest.Matchers;

import io.restassured.RestAssured;
import io.restassured.http.ContentType;

public class BDDStylePostRequest {
        
        
        public static void main(String[] args) {
                
                // There is no need to add escape character manually. Just paste string within double 
                // quotes. It will automatically add escape sequence as required. 
                String jsonString = "{\"username\" : \"admin\",\"password\" : \"password123\"}";
                
                
                //GIVEN
                RestAssured
                .given()
                        .baseUri("https://restful-booker.herokuapp.com/auth")
                        .contentType(ContentType.JSON)
                        .body(jsonString)
                // WHEN
                .when()
                        .post()
                // THEN
                .then()
                        .assertThat()
                        .statusCode(200)
                        .body("token", Matchers.notNullValue())
                        .body("token.length()", Matchers.is(15))
                        .body("token", Matchers.matchesRegex("^[a-z0-9]+$"));
                
        
        }

}

You can clone/download example repo here.

If you have any doubt, feel free to comment below.If you like my posts, please like, comment, share and subscribe.#ThanksForReading

#HappyLearning

Uncategorized

Post navigation

Previous Post: Part 6: Usages Of Javascripts In Selenium: Locating WebElements Using JavaScript Commands
Next Post: parameterised dataprovider

Related Posts

February 23, 2017 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
findElements Uncategorized
REST Assured Tutorial 21 – Get & Assert Response Time Of Request Uncategorized
October 4, 2017 – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark