Skip to content

Parsers and defaults

REST Assured providers predefined parsers for e.g. HTML, XML and JSON. But you can parse other kinds of content by registering a predefined parser for unsupported content-types by using:

RestAssured.registerParser(<content-type>, <parser>);

E.g. to register that mime-type ‘application/vnd.uoml+xml’ should be parsed using the XML parser do:

RestAssured.registerParser("application/vnd.uoml+xml", Parser.XML);

You can also unregister a parser using:

RestAssured.unregisterParser("application/vnd.uoml+xml");

Parsers can also be specified per “request”:

get(..).then().using().parser("application/vnd.uoml+xml", Parser.XML). ..;

and using a response specification.

Sometimes it’s useful to specify a default parser, e.g. if the response doesn’t contain a content-type at all:

RestAssured.defaultParser = Parser.JSON;

You can also specify a default parser for a single request:

get("/x").then().using().defaultParser(Parser.JSON). ..

or using a response specification.

By default REST assured assumes host localhost and port 8080 when doing a request. If you want a different port you can do:

given().port(80). ..

or simply:

..when().get("http://myhost.org:80/doSomething");

You can also change the default base URI, base path, port and authentication scheme for all subsequent requests:

RestAssured.baseURI = "http://myhost.org";
RestAssured.port = 80;
RestAssured.basePath = "/resource";
RestAssured.authentication = basic("username", "password");
RestAssured.rootPath = "x.y.z";

This means that a request like e.g. get("/hello") goes to: http://myhost.org:80/resource/hello with basic authentication credentials “username” and “password”. See rootPath for more info about setting the root paths. Other default values you can specify are:

RestAssured.filters(..); // List of default filters
RestAssured.requestSpecification = .. // Default request specification
RestAssured.responseSpecification = .. // Default response specification
RestAssured.urlEncodingEnabled = .. // Specify if Rest Assured should URL encoding the parameters
RestAssured.defaultParser = .. // Specify a default parser for response bodies if no registered parser can handle data of the response content-type
RestAssured.registerParser(..) // Specify a parser for the given content-type
RestAssured.unregisterParser(..) // Unregister a parser for the given content-type

You can reset to the standard baseURI (localhost), basePath (empty), standard port (8080), standard root path (""), default authentication scheme (none) and url encoding enabled (true) using:

RestAssured.reset();

reset() also unbinds the client that RestAssured.useClient(..) bound to the current thread. Since it resets the static settings for every thread, use RestAssured.resetClient() to only unbind the client in a parallel suite.

These static defaults are shared by all threads. When tests run in parallel, set them once before the tests start (for example in a @BeforeAll method) and don’t change them while requests are being made. Put values that differ between tests in the request specification instead, for example given().baseUri(..).port(..) or given(spec), or, if tests that run in parallel need different defaults, give each test a client of its own, as Parallel tests shows. Sending one request specification from many threads at once is safe, since REST Assured 7.0.0 sends a copy of it each time, but don’t change one specification from several threads at once. Make sure that filters added with RestAssured.filters(..) are thread safe.