Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the default header for all requests in apache http client?

For example the default user agent could be set like: client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, someName);
But how to set the "Accept" header?

like image 486
NSF Avatar asked Sep 09 '13 21:09

NSF


People also ask

How do I change HTTP headers?

After installing the extension, Go to app.requestly.io/rules.Then create rule and select "Modify Headers". After that give the rule name, and from here you can add, override and remove headers by providing a Header name and value for that header.


1 Answers

HttpClient 4.3 now allows configuring a collection of default headers on the client itself:

Header header = new BasicHeader(HttpHeaders.CONTENT_TYPE, "application/json");
List<Header> headers = Lists.newArrayList(header);
HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
HttpUriRequest request = RequestBuilder.get().setUri(SAMPLE_URL).build();
client.execute(request);

Now, all requests executed by that client will be send with the default headers. Hope that helps.

like image 150
Eugen Avatar answered Sep 24 '22 01:09

Eugen