12

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

2
  • Did you mean in the code level or in Apache configuration ? httpd.apache.org/docs/2.2/content-negotiation.html Commented Sep 9, 2013 at 22:00
  • I meant the httpclient library, not Apache server. Commented Sep 9, 2013 at 22:13

1 Answer 1

25

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.