Could not send request using restTemplate the same

2019-06-13 19:37发布

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

0条回答
登录 后发表回答