nikolas
nikolas

Reputation: 67

RestTemplate how remove utf8 from request?

hello i' ve the code from request a sistem but RestTemplate add always utf8 in header

example

POST /v1/documents/validation HTTP/1.1
Accept: application/json
Content-Type: multipart/form-data;charset=UTF8;boundary=gs7Tmph96b0PDkFCrOo9Y7EhtqqV3ok2agluTF

how remove charset=UTF8 ?

this is my code

 RestTemplate restTemplate = getRestTemplate();

            HttpHeaders headers = new HttpHeaders();

            headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
            headers.add("Content-Type", "multipart/form-data");

          
            headers.add("FSE-JWT-Signature", getHashSignature());
            headers.add("Authorization", "Bearer " + getBearerToken());


            LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();

            FileSystemResource value = new FileSystemResource(new File(fileName));
            map.add("file", value);
            map.add("requestBody", requestBody.toString());


            HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);

error

"title":"InvalidRequestContent","status":400,"detail":"Request content not conform to API specification: UTF-8;boundary=Kpj8KEbP1NBn3tLmsWgbj8O6LlcGNzyp60ejoi4A"

Upvotes: 0

Views: 390

Answers (1)

John Williams
John Williams

Reputation: 5440

By default RestTemplate adds the utf-8 if you use exchange(). I don't know why.

Use postForObject, ie something like this:

String serverEndPoint = "https://httpbin.org/post";
HttpHeaders headers = new HttpHeaders();
headers.setContentType("multipart/form-data");
HttpEntity<Whatever> entity = new HttpEntity<>(whateverObject, headers);
String result = restTemplate.postForObject(serverEndPoint, entity, String.class);

Upvotes: 0

Related Questions