More API requests
In this section, we will explore how to send advanced API requests using REST Assured. You will learn how to:
- Add headers, query parameters, and path parameters.
- Send POST, PUT, PATCH, and DELETE requests with JSON payloads.
- Handle different content types like JSON and XML.
Adding Headers, Query Parameters, and Path Parameters
In REST APIs, we often need to add extra information to our requests. Let’s break down these parts:
Adding Headers
Headers provide additional information about the request (e.g., content type, authentication).
Example: Setting the Content-Type header to application/json:
given() .header("Content-Type", "application/json") .when() .get("/books/20250401") .then() .statusCode(200);
Common Headers in REST APIs:
Header | Purpose |
---|---|
Content-Type | Tells the server the request format (e.g., JSON, XML) |
Authorization | Sends security tokens (e.g., Bearer token) |
Accept | Tells the server what response format you expect |
Adding Query Parameters
Query parameters are part of the URL and filter or modify the request.
Example: /books/?language=English(Get English Books))
In REST Assured:
given() .queryParam("language", "English") .when() .get("/books") .then() .statusCode(200);
Adding Path Parameters
Path parameters are part of the URL and represent specific resources.
Example: /books/20250401 (Get book with ISBN 20250401)
In REST Assured:
given() .pathParam("isbn", 20250401) .when() .get("/books/{isbn}") .then() .statusCode(200);
Sending POST, PUT, PATCH, and DELETE Requests
Now, let's work with these HTTP methods:
Sending a POST Request (Create Data)
Example: Add a new book with a JSON payload.
Step 1: Prepare the JSON Payload
String requestBody = """ { "name": "Book 2", "language": "English", "price": 200, "publisher": "ABC publisher" } """;
Step 2: Send the POST Request
given() .header("Content-Type", "application/json") .body(requestBody) .when() .post("/books") .then() .statusCode(201) // 201 = Created .body("name", equalTo("book 2"));
Sending a PUT Request (Update Data)
Example: Update all book details.
String updateBook = """ { "name": "book 2 Updated", "language": "Spanish", "price": 300.00, "publisher": "XYZ publisher" } """; given() .header("Content-Type", "application/json") .body(updateBook) .pathParam("isbn", 20250401) .when() .put("/books/{isbn}") .then() .statusCode(200) .body("name", equalTo("book 2 Updated"));
Sending a PATCH Request (Modify Data)
Example: Update only the name of a book.
String partialUpdate = """ { "name": "book 2 Patched" } """; given() .header("Content-Type", "application/json") .body(partialUpdate) .pathParam("isbn", 20250401) .when() .patch("/books/{isbn}") .then() .statusCode(200) .body("name", equalTo("Book 2 Patched"));
Sending a DELETE Request (Remove Data)
Example: Delete a book by ISBN.
given() .pathParam("isbn", 20250401) .when() .delete("/books/{id}") .then() .statusCode(204);
Handling Different Content Types (JSON and XML)
APIs may return data in various formats like JSON and XML. REST Assured can handle both.
Working with JSON Responses
Example: Validate a JSON response.
given() .accept("application/json") .when() .get("/books/20250401") .then() .statusCode(200) .body("name", equalTo("Book 1"));
Working with XML Responses
Some APIs return XML instead of JSON. REST Assured can handle XML validation using XPath.
Example XML Response:
<book> <id>20250401</id> <name>Book 1</name> <language>English</language> <price>200.00</price> <publisher>ABC publisher</publisher> </book>
Request XML Response
given() .accept("application/xml") .when() .get("/books/20250401") .then() .statusCode(200) .body("book.name", equalTo("Book 1"));
Combining Headers, Parameters, and Body
You can mix headers, query/path parameters, and body in one request.
Example: Update a book with query parameters and JSON body.
String updatedBook = """ { "name": "Updated Book" } """; given() .header("Content-Type", "application/json") .queryParam("language", "English") .pathParam("isbn", 20250401) .body(updatedbook) .when() .put("/books/{id}") .then() .statusCode(200) .body("name", equalTo("Updated Book"));
Error Handling and Validating Responses
When working with APIs, handle common errors like 404 (Not Found) and 400 (Bad Request).
Example: Handle a missing book.
given() .pathParam("isbn", 123) .when() .get("/books/{isbn}") .then() .statusCode(404);