-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Closed
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)type: bugA general bugA general bug
Milestone
Description
Background
/**
* Global option to specify a header to be added to every request,
* if the request does not already contain such a header.
*/
Builder defaultHeader(String header, String... values);WebClient and RestClient have default[Header|Cookie|Request|..] that useful to set default value on request.
Lines 502 to 508 in 899de4f
| private void initHeaders(HttpHeaders out) { | |
| if (!CollectionUtils.isEmpty(defaultHeaders)) { | |
| out.putAll(defaultHeaders); | |
| } | |
| if (!CollectionUtils.isEmpty(this.headers)) { | |
| out.putAll(this.headers); | |
| } |
As you can see above code, defaultHeader value is set only when it's not contained in headers.
Question
/**
* Provide a consumer to customize every request being built.
* @param defaultRequest the consumer to use for modifying requests
*/
Builder defaultRequest(Consumer<RequestHeadersSpec<?>> defaultRequest);Lines 481 to 484 in 899de4f
| private ClientRequest.Builder initRequestBuilder() { | |
| if (defaultRequest != null) { | |
| defaultRequest.accept(this); | |
| } |
But on defaultRequest(..), it can override request's values unlike defaultHeader cause it's called on every request being built.
Reproduce
// DefaultWebClientTests.java
@Test
void defaultRequest_override() {
ThreadLocal<String> context = new NamedThreadLocal<>("foo");
WebClient client = this.builder
.defaultRequest(spec -> spec.accept(MediaType.APPLICATION_JSON)) // default
.build();
client.get().uri("/path")
.accept(MediaType.IMAGE_PNG) // set
.retrieve().bodyToMono(Void.class).block(Duration.ofSeconds(10));
ClientRequest request = verifyAndGetRequest();
assertThat(request.headers().getAccept()).isEqualTo(MediaType.IMAGE_PNG); // ❌Expected: <image/png> but actual: <application/json>
}I think defaultRequest(spec -> spec.accept(MEDIA_TYPE)) will override all media types that set by accept(..) on this request.
Q. is above behavior of defaultRequest(..) is intended? thanks!
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
in: webIssues in web modules (web, webmvc, webflux, websocket)Issues in web modules (web, webmvc, webflux, websocket)type: bugA general bugA general bug
