0
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
headers.set("User-Agent", "Mozilla/5.0..."); //note - value hard coded

I am currently writing a JUnit test where I create a RestTemplate containing the above request headers.

I am hardcoding the user-agent header to be Mozilla currently, how can I add the actual user agent of from which the test is running here instead of hardcoding the value?

3
  • Just make something up if you need this header. Otherwise leave it undefined. Commented Oct 1, 2022 at 0:13
  • @AlexR why do you say this? Is not possible to get it programmatically? Commented Oct 1, 2022 at 12:33
  • Programatically from where? What do you expect the user-agent to be? Commented Oct 1, 2022 at 13:13

1 Answer 1

1

Every HTTP client (or so called user agent) can define its own value for the User-Agent HTTP header. All major HTTP clients, like for example browsers or command-line tools like cURL and wget, do so.

Since you are in effect writing your own HTTP client via RestTemplate, it is up to you whether to define a value for that header or leave it undefined. If you choose to define the header, it should follow the rules given in the HTTP specification (RFC 7231, or the current version RFC 9110).

To further answer your question: It seems like there was some discussion about setting a default value for that header for Spring Boot applications but it seems like the default value approach has not been implemented.

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.