Saverio Mirko Viola
Saverio Mirko Viola

Reputation: 85

Erase charset from java WebClient multiform-data post

We are making a post request in our spring boot application with Java WebClient. The request has MULTIPART_FORM_DATA as MediaType as you can see in the code below:

public Mono<ResponseEntity<String>> sendMultipartData(PersonRequestDTO personRequest) {
        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
        parts.add("name", personRequest.getNome());
        parts.add("surname", personRequest.getCognome());
        parts.add("fiscalCode", personRequest.getCodiceFiscale());

        return webClient.post()
                .uri(receiverUrl)
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromMultipartData("name", personRequest.getNome())
                        .with("surname", personRequest.getCognome())
                        .with("fiscalCode", personRequest.getCodiceFiscale()))
                .retrieve()
                .toEntity(String.class);
    }

Although this is an example of the real request, it is kept as simple as the original one.

Our problem is that when performing the post, a charset UTF-8 is set by default and this seems to be in contrast with some legacy systems which receive the call:

HTTP method: POST URI: /receive-multipart Headers: accept-encoding: gzip user-agent: ReactorNetty/0.9.2.RELEASE host: localhost:8080 accept: / transfer-encoding: chunked content-type: multipart/form-data;boundary=hw4mxNIYVkHevOoYWQtq-crk1nqxjyD5oez;charset=UTF-8 Parameters: name: Saverio surname: Viola fiscalcode fiscalCode

Basically, our webclient has a simple configuration:

public MultipartService() {
        this.webClient = WebClient.builder().build();      
}

spring-starter-webflux version is 2.2.2.RELEASE.

The question is: is there any way to erase the charset from contentType in webClient By means of strategies?

I've managed to accomplish the task by means of java RestRemplate this way:

@Service
public class MultipartService {

    private final RestTemplate restTemplate;

    @Value("${receiver.url}")
    private String receiverUrl;

    public MultipartService() {
       
        this.restTemplate = new RestTemplate();

        MyFormHttpMessageConverter myFormHttpMessageConverter = new MyFormHttpMessageConverter();
        myFormHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));

        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        stringHttpMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));

        List<HttpMessageConverter<?>> converters = List.of(myFormHttpMessageConverter, stringHttpMessageConverter);
        this.restTemplate.setMessageConverters(converters);
    }

    public ResponseEntity<String> sendMultipartData(PersonRequestDTO personRequest) {
        MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>();
        parts.add("name", personRequest.getNome());
        parts.add("surname", personRequest.getCognome());
        parts.add("fiscalCode", personRequest.getCodiceFiscale());

        //HTTP header without charset
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_TYPE, "multipart/form-data"); 
      

        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(parts, headers);
        return restTemplate.exchange(receiverUrl, HttpMethod.POST, requestEntity, String.class);
    }

}

where MyFormHttpMessageConverter does the job.

Does anyone knows if there is an equivalent in webClient? I found nothing on the web...

Thanks for your help.

Upvotes: 0

Views: 33

Answers (0)

Related Questions