I’m facing something strange. With Spring Boot 3.2.0 I’m trying to call a webservice expecting a Multipart form data.
The same code using RestClient only works when the code is intercepted through a RequestInterceptor. Only calling the execution method in the interceptor is enough to make the code work. Otherwise I get an error saying that the request is missing the “file” part.
I did not include the code of the logging part because it seems useless regarding the behaviour. But of course we could see the “file” part included in the request (otherwise it would not work with this code)
public void send(ByteArrayResource byteArrayResource, Object otherObject) {
RestClient restClient = RestClient.builder()
.requestInterceptor(new RequestInterceptor()) // when commented not working
.baseUrl("https://url.webservice.com")
.defaultHeader("AUTH_HEADER", "AUTH_VALUE")
.build();
// create multipart form data
MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
parts.add("file", byteArrayResource);
parts.add("otherObject", otherObject);
return restClient.post()
.uri(createPostConfig.urlSuffix())
.body(parts)
.retrieve()
.body(String.class);
}
public static class RequestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
return execution.execute(request, body);
}
}
user112707 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.