0

I have http template which works from Postman:

enter image description here

To execute the same request using java code I wrote following code:

LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
exchange.getIn().getHeader(Exchange.FILE_NAME_ONLY, String.class);
map.add("file", new File("filePath");
int lastIndexOfDot = fileName.lastIndexOf(".");
map.add("type", fileName.substring(lastIndexOfDot + 1));
map.add("org_id", systemSettingsService.getSystemSettings().getOrganizationId());
map.add("stone_id", fileName.substring(0, lastIndexOfDot));

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>( map, headers);

restTemplate = new RestTemplate();                     
try {
    ResponseEntity<String> result = restTemplate.exchange(buildUrl(), HttpMethod.POST, requestEntity, String.class);
    logger.info("result {}", result);
 } catch (Exception e) {
     logger.error("Error", e);
 }

and in logs I see:

org.springframework.web.client.HttpClientErrorException: 400 BAD REQUEST
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:78)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:700)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:653)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:531)

I used fidddler to monitor the request but I was able to monitor only request from postman.

It looks like this(header):

enter image description here

raw:

enter image description here

How to correct my java code to make request the same as in the postman?

P.S.

I use the same values for http attributes as in postman

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710
  • Can it be that your system firewall blocks java from sending requests? Try to trace the request on java side https://stackoverflow.com/q/7952154/2065796. It could also help to paste your code into a unit test and check the same with `TestRestTemplate ` - it has very detailed output and you might notice what you didn't notice on real run. – Sasha Shpota Jan 12 '18 at 19:47
  • @Oleksandr Shpota, looks like it is not firewall because I tried to change stone_id with stoneId in my code and at this cse service return 200 but with errornous body – gstackoverflow Jan 12 '18 at 20:44
  • 1
    I'm not sure that a `File` will end up being translated as the content of that file. Use a `FileSystemResource` (or any other appropraite `Resource`). – Sotirios Delimanolis Jan 13 '18 at 21:22
  • @Sotirios Delimanolis, thank you very much! it works. Please, add answer - I will accept it – gstackoverflow Jan 14 '18 at 10:08

0 Answers0