Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,8 @@ private Mono<ResponseEntity<T>> exchange(RequestEntity<?> requestEntity) {
Objects.requireNonNull(requestEntity.getMethod(), "Method must not be null");
RequestBodySpec builder = rest.method(requestEntity.getMethod())
.uri(requestEntity.getUrl())
.headers(headers -> addHeaders(headers, requestEntity.getHeaders()));
.headers(headers -> addHeaders(headers, requestEntity.getHeaders()))
.headers(headers -> addHeaders(headers, exchange.getRequest().getHeaders()));
WebClient.ResponseSpec result;
if (requestEntity.getBody() instanceof Publisher) {
@SuppressWarnings("unchecked")
Expand All @@ -384,12 +385,10 @@ else if (requestEntity.getBody() != null) {
}
else {
if (hasBody) {
result = builder.headers(headers -> addHeaders(headers, exchange.getRequest().getHeaders()))
.body(exchange.getRequest().getBody(), DataBuffer.class)
.retrieve();
result = builder.body(exchange.getRequest().getBody(), DataBuffer.class).retrieve();
}
else {
result = builder.headers(headers -> addHeaders(headers, exchange.getRequest().getHeaders())).retrieve();
result = builder.retrieve();
}
}
return result.onStatus(HttpStatusCode::isError, t -> Mono.empty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ public void post() throws Exception {
.isEqualTo("host=localhost:" + port + ";foo");
}

@Test
public void postWithBodyForwardsIncomingHeaders() throws Exception {
ResponseEntity<Bar> response = rest
.exchange(RequestEntity.post(rest.getRestTemplate().getUriTemplateHandler().expand("/proxy/0"))
.header("X-Custom", "incoming-header-value")
.contentType(MediaType.APPLICATION_JSON)
.body(Collections.singletonMap("name", "foo")), Bar.class);
assertThat(response.getBody().getName()).contains("incoming-header-value");
}

@Test
public void postJsonWithWhitespace() {
var json = """
Expand Down