Quantcast
Channel: Active questions tagged charset - Stack Overflow
Viewing all articles
Browse latest Browse all 59

Erase charset from java WebClient multiform-data post

$
0
0

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: POSTURI: /receive-multipartHeaders:accept-encoding: gzipuser-agent: ReactorNetty/0.9.2.RELEASEhost: localhost:8080accept: /transfer-encoding: chunkedcontent-type: multipart/form-data;boundary=hw4mxNIYVkHevOoYWQtq-crk1nqxjyD5oez;charset=UTF-8Parameters:name: Saveriosurname: Violafiscalcode 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:

@Servicepublic 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.


Viewing all articles
Browse latest Browse all 59

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>