Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Content-Length when body changed by Interceptor #33459

Closed
wants to merge 3 commits into from
Closed
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 @@ -88,6 +88,16 @@ public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOExc
return nextInterceptor.intercept(request, body, this);
}
else {

/*
* After intercepted by the interceptors, the length of the body may change.
* Set the latest body.length to "Content-Length"
*/
long contentLength = request.getHeaders().getContentLength();
if (contentLength > -1 && contentLength != body.length) {
request.getHeaders().setContentLength(body.length);
}

HttpMethod method = request.getMethod();
ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method);
request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ void changeBody() throws Exception {
ClientHttpRequest request = requestFactory.createRequest(URI.create("https://example.com"), HttpMethod.GET);
request.execute();
assertThat(Arrays.equals(changedBody, requestMock.getBodyAsBytes())).isTrue();

// When the request body is changed, the "Content-Length" in the request header also needs to be changed.
assertThat(requestMock.getHeaders().getContentLength()).isEqualTo(changedBody.length);
}


Expand Down