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
- GET
/acceptwith anAcceptheader ofapplication/json. - Read the
formatfield with JsonPath and print it afterJSON format:. - GET
/acceptagain, this time withAcceptset toapplication/xml. - Take the body as a string, check whether it contains the
formatelement 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 withformatset 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 toxmlPath(), 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().
Related lessons
- Validate Content-Type with MatchersAsserts that a response is JSON or XML using the
ContentTypeenum. - Extract from XML with XmlPathReads values out of an XML response with
XmlPathand GPath. - Get and Validate XML ResponseChecks a raw XML body as a string, like the second request here.
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.
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>"
}
}