Skip to content
CodeKruPro

Content Negotiation

28/62

Content Negotiation

The Accept header tells the server what format you want. The /accept endpoint responds with JSON or XML based on the Accept header.

Task:

  1. Request JSON by sending Accept: application/json, print the format field
  2. Request XML by sending Accept: application/xml, verify the body contains XML

Expected output:

JSON format: json
XML response contains format tag: true

About this lesson

REST Assured content negotiation with the Accept header: JSON or XML

Learn REST AssuredIntermediateHeaders

How it works

Content negotiation lets one URI serve several formats. The client lists the media types it can handle in the Accept request header, and the server picks one and labels the reply with a matching Content-Type. The resource stays the same; only the representation changes.

The testkru /accept endpoint switches to XML when Accept contains application/xml, or when the query string asks for the XML format. Everything else gets JSON, including REST Assured's default wildcard and, notably, text/xml, which this server does not recognise. The XML answer is a small response document with message and format elements.

You can set the header by hand with header(), or use accept() with the ContentType enum. Be aware that accept(ContentType.XML) sends three XML media types in one header, not a single one, and that a repeated Accept replaces the earlier value. To read an XML reply, use xmlPath(). Reading a field from an XML body through jsonPath() throws a JsonPathException.

About the assignment

Ask the same endpoint twice, once for JSON and once for XML, and show that the server honoured each request.

What you will do

  1. GET /accept with an Accept header of application/json.
  2. Read the format field with JsonPath and print it after JSON format:.
  3. GET /accept again, this time with Accept set to application/xml.
  4. Take the body as a string, check whether it contains the format element with the value xml, and print that boolean after the label from the starter comment.

Common mistakes

  • Asking for text/xml

    It is a valid XML media type, but this endpoint only looks for application/xml, so the reply is JSON with format set to json. Content negotiation depends on what the server understands, not on what is standard.

  • Parsing XML as JSON

    Reusing the JSON reading code on the second response throws a JsonPathException. Switch to xmlPath(), or to a plain string check as the task does.

  • Forgetting the default Accept

    With no header set, REST Assured still sends a wildcard Accept. A server is free to choose any format for that, so do not rely on the default in a test that cares about the format.

In a real test suite

APIs that serve both formats need tests for each, and the cleanest check is on the response Content-Type rather than the body text. Lesson 29 asserts that directly with then().contentType().

To try one of these calls on any API endpoint without a task, open the Online Selenium Java Compiler (REST Assured is included).

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import static io.restassured.RestAssured.*;

public class Main {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://testkru.com/api";

        // TODO: GET /accept with header Accept: application/json
        // Print "JSON format: " + the "format" field from the response

        // TODO: GET /accept with header Accept: application/xml
        // Print "XML response contains format tag: " + body contains "<format>xml</format>"
    }
}