Atharva Kulkarni
Atharva Kulkarni

Reputation: 1

I'm getting error "org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request"

I'm getting this error "org.springframework.web.client.HttpClientErrorException$BadRequest: 400 Bad Request" while trying to internally call POST API from another GET API in spring boot. following is the code. also I'm getting multipart file as input in the get api and wants to send that file to this internal post api

Path tempFile = Files.createTempFile(null, null);

            Files.write(tempFile, newFile.getBytes());
            File fileToSend = tempFile.toFile();

            FileSystemResource fr = new FileSystemResource(fileToSend);

            String baseUrl = "someURL which is correct";
            URI uri = new URI(baseUrl);
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.MULTIPART_FORM_DATA);
            //headers.set("Content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
            headers.set("Ocp-Apim-Subscription-Key","{key which i dont want to display}");

            MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
            body.add("newFile", fr);

            HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);

            RestTemplate restTemplate = new RestTemplate();
            ResponseEntity<String> response = restTemplate.postForEntity(uri, requestEntity, String.class);

            return response;

Upvotes: 0

Views: 710

Answers (1)

Naveen Purmani
Naveen Purmani

Reputation: 11

Pass baseUrl string instead of uri in restTemplate.postForEntity method and try

Upvotes: 1

Related Questions