Default request headers

3,303 views
Skip to first unread message

Juraj Martinka

unread,
Sep 3, 2013, 9:53:13 AM9/3/13
to rest-a...@googlegroups.com
Hi, 
I wonder if it's possible to set default request headers for all requests.
To be specific, I want to set "User-Agent" header for all requests in a following method (called at the tests setup phase):
static configure(String host, String user, String password) {
        baseURI = host
        authentication = preemptive().basic(user, password);
        requestContentType(ContentType.JSON)
        defaultParser = Parser.JSON
        // TODO:
        // set default User-Agent request header
}

AFAIK, there is no (easy) way how to achieve this with rest-assured.
I checked com.jayway.restassured.internal.RequestSpecificationImpl#setRequestHeadersToHttpBuilder and found no way hot set headers globally.
Am I missing something?

PS: With HTTPBuilder it's quite easy:
            httpBuilder.defaultRequestHeaders.put('User-Agent', myUserAgent)

Johan Haleby

unread,
Sep 3, 2013, 10:10:37 AM9/3/13
to rest-a...@googlegroups.com
Hi, 

Yes this is possible by using a Request Specification:

RequestSpecification requestSpecification = new RequestSpecBuilder().addHeader("User-Agent", myUserAgent).build();

Once you have created the specification and you really want to apply it to ALL requests you can do like this:

RestAssured.requestSpecification = requestSpecification;

But you can also apply it in your setup:

given().spec(requestSpecification). ...

Regards,
/Johan


--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Anil Shekhar

unread,
Jun 17, 2016, 11:50:57 AM6/17/16
to REST assured
This is awesome. Thanks Johan.
Reply all
Reply to author
Forward
0 new messages