๐ฆ Handling Headers and Request Body in RestAssured
๐ Introductionโ
Headers and request bodies are critical components of API requests. Headers provide metadata about the request, such as content type, authentication details, or caching instructions. The request body, on the other hand, carries the data being sent to the server, typically in JSON or XML format.
In this section, weโll explore:
- Headers: How to add and validate headers in your API tests
- Request Body: How to construct and send request bodies for POST, PUT, and PATCH requests
- Best practices for handling headers and request bodies effectively
๐ Headersโ
What Are Headers?โ
Headers are key-value pairs included in HTTP requests and responses. They provide additional information about the request or response, such as:
- Content-Type: Specifies the format of the data being sent (e.g.,
application/json) - Authorization: Sends credentials or tokens for authentication
- Accept: Indicates the format of the response the client expects
How to Add Headers in RestAssuredโ
Single Headerโ
given()
.header("Content-Type", "application/json")
.when()
.get("/users");
Multiple Headersโ
given()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer <token>")
.when()
.get("/users");
Dynamic Headersโ
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...";
given()
.header("Authorization", "Bearer " + token)
.when()
.get("/users");
Validating Headers in Responsesโ
given()
.when()
.get("/users")
.then()
.header("Content-Type", "application/json")
.header("Cache-Control", "no-cache");
๐งพ Request Bodyโ
What Is a Request Body?โ
The request body contains the data being sent to the server, typically in JSON or XML format. It is used in POST, PUT, and PATCH requests to create or update resources.
How to Add a Request Body in RestAssuredโ
JSON Request Bodyโ
String requestBody = "{ "name": "John", "age": 30 }";
given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.post("/users")
.then()
.statusCode(201);
Using POJOs (Plain Old Java Objects)โ
public class User {
private String name;
private int age;
// Getters and Setters
}
User user = new User();
user.setName("John");
user.setAge(30);
given()
.header("Content-Type", "application/json")
.body(user)
.when()
.post("/users")
.then()
.statusCode(201);
Dynamic Request Bodiesโ
String name = "John";
int age = 30;
String requestBody = "{ "name": "" + name + "", "age": " + age + " }";
given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.post("/users")
.then()
.statusCode(201);
Validating the Response Bodyโ
String requestBody = "{ "name": "John", "age": 30 }";
given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.post("/users")
.then()
.statusCode(201)
.body("name", equalTo("John"))
.body("age", equalTo(30));
๐ Combining Headers and Request Bodyโ
String requestBody = "{ "name": "John", "age": 30 }";
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...";
given()
.header("Content-Type", "application/json")
.header("Authorization", "Bearer " + token)
.body(requestBody)
.when()
.post("/users")
.then()
.statusCode(201)
.body("name", equalTo("John"))
.body("age", equalTo(30));
โ Best Practices for Handling Headers and Request Bodyโ
-
Use Consistent Headers
-
Validate Headers and Response Body
-
Externalize Request Data
File requestBody = new File("src/test/resources/user.json");
given()
.header("Content-Type", "application/json")
.body(requestBody)
.when()
.post("/users");
- Reuse Common Configurations
private RequestSpecification withAuthHeader(String token) {
return given()
.header("Authorization", "Bearer " + token);
}
- Log Requests and Responses
given()
.log().all()
.header("Content-Type", "application/json")
.body("{ "name": "John", "age": 30 }")
.when()
.post("/users")
.then()
.log().all();
๐ Conclusionโ
Headers and request bodies are essential for constructing and validating API requests. By mastering how to handle them in RestAssured, you can write robust and flexible API tests. In the next section, weโll explore JSON Path extraction and logging to help you extract and debug specific fields from API responses.