How to set multiple headers at once in Spring WebClient?

32,714

Solution 1

If those headers change on a per request basis, you can use:

webClient.get().uri("/resource").headers(httpHeaders -> {
    httpHeaders.setX("");
    httpHeaders.setY("");
});

This doesn't save much typing; so for the headers that don't change from one request to another, you can set those as default headers while building the client:

WebClient webClient = WebClient.builder().defaultHeader("...", "...").build();
WebClient webClient = WebClient.builder().defaultHeaders(httpHeaders -> {
    httpHeaders.setX("");
    httpHeaders.setY("");
}).build();

Solution 2

The consumer is correct, though it's hard to visualize, esp. in that you can continue with additional fluent-composition method calls in the webclient construction, after you've done your work with the headers.

....suppose you have a HttpHeaders (or MutliValue map) holding your headers in scope. here's an example, using an exchange object from spring cloud gateway:

final HttpHeaders headersFromExchangeRequest = exchange.getRequest().headers();
webclient.get().uri("blah-blah")
    .headers( httpHeadersOnWebClientBeingBuilt -> { 
         httpHeadersOnWebClientBeingBuilt.addAll( headersFromExchangeRequest );
    }
)...

the addAll can take a multivalued map. if that makes sense. if not, let your IDE be your guide.

to make the consumer clearer, let's rewrite the above as follows:

private Consumer<HttpHeaders> getHttpHeadersFromExchange(ServerWebExchange exchange) {
    return httpHeaders -> {
        httpHeaders.addAll(exchange.getRequest().getHeaders());
    };
}
.
.
.
webclient.get().uri("blah-blah")
    .headers(getHttpHeadersFromExchange(exchange))
    ...
Share:
32,714
Admin
Author by

Admin

Updated on December 30, 2021

Comments

  • Admin
    Admin over 2 years

    I was trying to set headers to my rest client but every time I have to write

    webclient.get().uri("blah-blah")
             .header("key1", "value1")
             .header("key2", "value2")...
    

    How can I set all headers at the same time using headers() method?

  • Lahiru Liyanage
    Lahiru Liyanage over 3 years
    cannot understand httpHeaders -> {} part.. can you explain it with a proper example please?
  • PlickPlick
    PlickPlick over 2 years
    This is a proper example actually it is a really really good one. If you do not understand it you probably need to look into lambda and functional interfaces. If just starting out do not expect it to be a quick thing to learn it is hard to grasp first time around it :)
  • Christian Blanco
    Christian Blanco over 2 years
    @PlickPlick, do you know if there is something that we can use as network interceptor in case the request of the WebClient returns a 401 UNAUTHORIZED? so we can call a logic to refresh tokens and then recall the last call?
  • PlickPlick
    PlickPlick over 2 years
    Check onSuccess onError and onErrorResume. baeldung.com/spring-webflux-errors
  • PlickPlick
    PlickPlick over 2 years
    But I'm actually not really sure what you mean by network interceptor. Also I have resently started using webclients so probably not the right guy to ask 🙂