How to Add Content-Type to Request in Rest Assured

When working with REST APIs, specifying the Content-Type header is crucial as it tells the server the format of the data being sent in the request body. In Rest Assured, adding the Content-Type header is straightforward. In this blog post, we’ll explore why this header is important and demonstrate how to include it in your requests.


Why Is Content-Type Important?

The Content-Type header defines the type of the data being sent to the server. For example:

  • application/json indicates JSON data.
  • application/xml indicates XML data.
  • text/plain indicates plain text.

If the server expects JSON but receives XML or plain text, the request might fail or behave unexpectedly. To ensure smooth communication with the API, always specify the appropriate Content-Type.


Adding Content-Type in Rest Assured

In Rest Assured, the Content-Type can be set using one of the following methods:

1. Using .contentType() Method

Rest Assured provides a built-in method to set the Content-Type. Here's an example of a POST request sending JSON data:

import io.restassured.RestAssured;

import static io.restassured.RestAssured.*;


public class ContentTypeExample {

    public static void main(String[] args) {

        RestAssured.baseURI = "https://example.com/api";


        given()

            .contentType("application/json") // Set Content-Type

            .body("{\"name\": \"John\", \"age\": 30}") // JSON payload

        .when()

            .post("/users") // Endpoint

        .then()

            .statusCode(201) // Validate response status

            .log().all(); // Log response

    }

}

2. Using .header() Method

If you want more control or need to set multiple headers, you can use the .header() method:

import io.restassured.RestAssured;

import static io.restassured.RestAssured.*;


public class ContentTypeHeaderExample {

    public static void main(String[] args) {

        RestAssured.baseURI = "https://example.com/api";


        given()

            .header("Content-Type", "application/json") // Set Content-Type header explicitly

            .body("{\"name\": \"Jane\", \"age\": 25}") // JSON payload

        .when()

            .post("/users")

        .then()

            .statusCode(201)

            .log().all();

    }

}

3. Setting Content-Type in Static Configuration

If you frequently use the same Content-Type, you can configure it globally for all requests:

import io.restassured.RestAssured;


public class GlobalContentTypeExample {

    public static void main(String[] args) {

        // Set default Content-Type globally

        RestAssured.config = RestAssured.config()

            .headerConfig(headerConfig().overwriteHeadersWithName("Content-Type"));


        RestAssured.defaultParser = Parser.JSON;


        RestAssured.baseURI = "https://example.com/api";


        given()

            .body("{\"name\": \"Emma\", \"age\": 35}")

        .when()

            .post("/users")

        .then()

            .statusCode(201)

            .log().all();

    }

}

Key Points to Remember

  • Choose the correct Content-Type: Use application/json for JSON APIs, application/xml for XML, and so on.
  • Be explicit: Explicitly setting the Content-Type avoids potential errors, especially when working with APIs requiring strict format validation.
  • Global Configuration: Use global settings if you want to avoid repetitive code for frequently used headers.

Followers