Table of contents
1.
Introduction
2.
Multiple Ways of Calling HTTP Methods on a RequestSpecification
3.
About HTTP methods
4.
Implementation
5.
Key Takeaways
6.
Frequently Asked Questions
6.1.
What can you not do with a RequestSpecification in REST assured?
6.2.
Can you explain the RequestSpecification request RestAssured given?
6.3.
What HTTP methods does REST assured support?
6.4.
What is the difference between RequestSpecBuilder and RequestSpecification?
6.5.
Why do we use static import in Rest assured?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

REST Assured – Multiple Ways of Calling HTTP Methods on a RequestSpecification

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

Introduction

In this article, we will learn about calling HTTP methods (get, post, put, and so on) on a RequestSpecification.

RequestSpecification

Rest Assured's main confusing and exciting aspect is that there are multiple ways to perform the same action, which needs to be clarified for beginners. Rest Assured is jam-packed with syntactic sugar techniques.

Multiple Ways of Calling HTTP Methods on a RequestSpecification

Here we will learn about 3 ways of Calling HTTP Methods on a RequestSpecification.

1. We can directly call GET, POST, PUT and DELETE on RequestSpecification [These GET, POST… are called as HTTP verbs]

2. Calling HTTP verbs after we pass the RequestSpecification reference variable in the overloaded given() method.

3. Calling HTTP verbs after we pass the RequestSpecification reference variable in spec().

About HTTP methods

The HTTP requests are spread across several tutorials, a consolidated reference can serve as a bookmark for our readers as they get their hands dirty. With rest assured. In this short post, we've combined rest-assured examples with various HTTP requests, each with hands-on code. 

This is also reflected in the index below:

  • HTTP GET Request Implementation
     
  • POST Request Implementation
     
  • HTTP PUT Request Implementation
     
  • HTTP DELETE Request Implementation
     

Rest Assured API supports HTTP Requests such as GET, POST, PUT, and DELETE. Let's review these methods before going over how to implement them in Rest Assured.

  • HTTP GET Request implementation: The HTTP GET request is commonly used in the API world. This request can fetch/get a resource from a server without sending any data. Because this post is only about examples, you can refer to the GET request post here.
     
  • HTTP POST Request Implementation: If we want to post/send data to the server or create a resource on the server, we should use an HTTP POST request. Understanding the HTTP POST Request Method Using Rest Assured can provide more information on the subject.
     
  • HTTP PUT Request Implementation: The HTTP PUT request either updates a resource or replaces the target resource's representation with the entire JSON request payload. PUT Request Using Rest Assured has a detailed discussion of HTTP PUT requests.
     
  • HTTP DELETE Request Implementation: A DELETE request can remove a resource from a server. The article DELETE Request Using Rest Assured contains a detailed description of the DELETE request.

Implementation

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


public class CallingHTTPMethods {
public static void main(String[] args) {
		/*GET */
		RequestSpecification objReqSpec1= RestAssured.given();

		Response response11 = objReqSpec1.get("http://localhost:8080/yuvati1/");
		System.out.println(response11.asString());

		Response response12 = RestAssured.given(objReqSpec1).get("http://localhost:8080/yuvati2/");
		System.out.println(response12.asString());

		Response response13 = RestAssured.given().spec(objReqSpec1).get("http://localhost:8080/yuvati3/");
		System.out.println(response13.asString());

		System.out.println("                                                              ");

		/* POST */
		String jsonStringC = "{\"name\" : \"ABC\",\"title\" : \"INTERN\"}";

		RequestSpecification objReqSpec2= RestAssured.given();
		objReqSpec2.contentType(ContentType.JSON);
		objReqSpec2.baseUri("https://reqres.in/api/users");
		objReqSpec2.body(jsonStringC);

		Response response21 = objReqSpec2.post();
		System.out.println(response21.asString());

		Response response22 = RestAssured.given(objReqSpec2).post();
		System.out.println(response22.asString());

		Response response23 = RestAssured.given().spec(objReqSpec2).post();
		System.out.println(response23.asString());

		System.out.println("                                                              ");

		/* PUT */
		String jsonStringU = "{\"name\" : \"XYZ\",\"title\" : \"INTERN\"}";

		RequestSpecification objReqSpec3= RestAssured.given();
		objReqSpec3.contentType(ContentType.JSON);
		objReqSpec3.baseUri("https://reqres.in/api/users/2");
		objReqSpec3.body(jsonStringU);

		Response response31 = objReqSpec3.put();
		System.out.println(response31.asString());

		Response response32 = RestAssured.given(objReqSpec3).put();
		System.out.println(response32.asString());

		Response response33 = RestAssured.given().spec(objReqSpec3).put();
		System.out.println(response33.asString());

		System.out.println("                                                              ");

		/*DELETE */
		String jsonStringX = "{\"name\" : \"NAME\",\"title\" : \"TITLE\"}";

		RequestSpecification objReqSpec4= RestAssured.given();
		objReqSpec4.contentType(ContentType.JSON);
		objReqSpec4.baseUri("https://reqres.in/api/users/2");
		objReqSpec4.body(jsonStringX);

		Response response41 = objReqSpec4.delete();
		System.out.println("Status Code1: "+response41.getStatusCode());

		Response response42 = RestAssured.given(objReqSpec4).delete();
		System.out.println("Status Code2: "+response42.getStatusCode());

		Response response43 = RestAssured.given().spec(objReqSpec4).delete();
		System.out.println("Status Code3: "+response43.getStatusCode());
	}
}
You can also try this code with Online Java Compiler
Run Code


Output:

output

In the above code, at first, we directly called all the HTTP methods(GET, POST, PUT, DELETE) on RequestSpecification then, in a second way, we called HTTP methods after we have passed RequestSpecification reference variable in overloaded given() method. Thirdly, we have called HTTP methods after we pass the RequestSpecification reference variable in the spec() method.

Key Takeaways

This post gave programming examples of various HTTP requests using the REST Assured framework. By clicking on the link provided in the explanation of each method above, you may view the details of each request.

  • An HTTP GET request is made to the server to retrieve a specific resource.
     
  • The HTTP POST request posts or sends information to the server or creates a new resource.
     
  • The HTTP PUT request either updates a specific resource or replaces the target resource's representation.
     
  • An HTTP DELETE request removes a particular resource from the server.
     
  • Rest Assured can programmatically simulate each of the methods listed above.

Frequently Asked Questions

What can you not do with a RequestSpecification in REST assured?

RequestSpecification is an interface that cannot be turned into an object in REST assured. 

Can you explain the RequestSpecification request RestAssured given?

The RequestSpecification interface that Rest Assured provides is used to club and extract repetitive actions like setting up base URLs, headers, HTTP verbs, etc., which may be shared for multiple Rest calls.

What HTTP methods does REST assured support?

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

What is the difference between RequestSpecBuilder and RequestSpecification?

You can use the builder to create a request specification using RequestSpecBuilder, while RequestSpecification lets you specify how the request should look.

Why do we use static import in Rest assured?

A static import allows you to use public and static members of a class, such as fields and methods, in Java code without mentioning the class name.

Conclusion

This article discussed multiple ways of calling HTTP methods (get, post, put, and so on) on a RequestSpecification.

Refer to our guided path on code studio to learn more about DSA Competitive Programming, Javascript System Design, etc. Enroll in our courses and refer to the mock test and problems available. Also, look at the interview experiences for placement preparations.

Happy Learning, Ninjas.

Live masterclass