0% found this document useful (0 votes)
3 views

RestAssured Notes

The document explains how REST Assured works for interacting with APIs through HTTP requests, detailing the creation of requests, HTTP methods, headers, payload, and response validation. It emphasizes the BDD approach used in REST Assured for writing test cases in a readable format. A demo is provided for sending a GET request and printing response details such as status code, body, headers, and response time.

Uploaded by

abhisharma4it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

RestAssured Notes

The document explains how REST Assured works for interacting with APIs through HTTP requests, detailing the creation of requests, HTTP methods, headers, payload, and response validation. It emphasizes the BDD approach used in REST Assured for writing test cases in a readable format. A demo is provided for sending a GET request and printing response details such as status code, body, headers, and response time.

Uploaded by

abhisharma4it
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

How REST Assured Works

1. Creating an HTTP Request


To interact with an API, an HTTP request must be created with all necessary details.
Example:
https://reqres.in/api/users/2
 Base URL: https://reqres.in
 Resource: api/users/
 Path Parameter: 2
 Query Parameter:
o Example: https://reqres.in/api/users?page=2
2. HTTP Methods/Verbs
CRUD (Create, Read, Update, Delete) operations in RESTful APIs are handled using different HTTP methods:
 POST: Used to create a new resource on the server.
 GET: Used to retrieve data from the server.
 PUT/PATCH: Used to update an existing resource.
 DELETE: Used to remove a resource from the server.
3. Headers
Headers contain metadata about the HTTP request or response. They serve various purposes, including:
 Authentication
 Caching
 Handling cookies
 Providing message body information
Headers are typically key-value pairs or can have multiple values per key.
4. Payload/Body
The Payload/Body carries data sent to the server in POST, PUT, or PATCH requests. This is used for:
 Creating new resources
 Updating existing resources
5. Sending the Request
Once all the required details are included, the request is sent over the network.
6. Validating the Response
After receiving the response, validation is performed by checking:
 Status Code
 Response Headers
 Response Body
HTTP Status Codes:
 100-199: Informational – The request is received, and processing is continuing.
 200-299: Success – The request was successfully processed.
 300-399: Redirection – Further action is required to complete the request.
 400-499: Client Error – There was an error from the client side.
 500-599: Server Error – The server encountered an error processing the request.
7. REST Assured and BDD (Behavior Driven Development) Support
REST Assured follows the BDD (Behavior Driven Development) approach, where test cases are written in an easy-to-
read format.
BDD Format in REST Assured:
 given() – Define all request details (Base URI, Headers, Path/Query Parameters, Payload).
 when() – Specify the HTTP method (POST, GET, PUT, PATCH, DELETE).
 then() – Validate the response (status code, headers, body, etc.).

Demo
GET Request - https://reqres.in/api/users?page=2
Print response code, response body, response headers, and response time.
URL: https://reqres.in/api/users?page=2
Query Parameter: page=2
This means it fetches user data from the second page.
Expected Output:

The response will include:

1. Response Code (e.g., 200 OK for a successful request).


2. Response Body (JSON data containing user details).
3. Response Headers (Metadata about the response).
4. Response Time (How long the request took).

package session03;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.response.Response;

public class TestGetMethod {

@Test
public void test01() {
// Sending GET request
Response response = RestAssured.get("https://reqres.in/api/users?page=2");

// Printing Response Details


System.out.println("Response Code: " + response.getStatusCode());
System.out.println("Response Body: " + response.getBody().asPrettyString());
System.out.println("Response Time: " + response.getTime() + " ms");
System.out.println("Response Header (Content-Type): " + response.getHeader("Content-Type"));
}
}

You might also like