Skip to content

Support StreamingHttpOutputMessage in RestClient #35102

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

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 @@ -477,7 +477,14 @@ public <T> RequestBodySpec body(T body, ParameterizedTypeReference<T> bodyType)

@Override
public RequestBodySpec body(StreamingHttpOutputMessage.Body body) {
this.body = request -> body.writeTo(request.getBody());
this.body = request -> {
if (request instanceof StreamingHttpOutputMessage streamingMessage) {
streamingMessage.setBody(body);
}
else {
body.writeTo(request.getBody());
}
};
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.web.client;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -559,6 +560,27 @@ void postUserAsJsonWithJsonView(ClientHttpRequestFactory requestFactory) {
});
}

@ParameterizedRestClientTest
void postStreamingMessageBody(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);

prepareResponse(response -> response.setResponseCode(200));

ResponseEntity<Void> result = this.restClient.post()
.uri("/streaming/body")
.body(new ByteArrayInputStream("test-data".getBytes(UTF_8))::transferTo)
.retrieve()
.toBodilessEntity();

assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);

expectRequestCount(1);
expectRequest(request -> {
assertThat(request.getPath()).isEqualTo("/streaming/body");
assertThat(request.getBody().readUtf8()).isEqualTo("test-data");
});
}

@ParameterizedRestClientTest // gh-31361
public void postForm(ClientHttpRequestFactory requestFactory) {
startServer(requestFactory);
Expand Down
Loading