Could not send request using restTemplate the same as in the postman application

一曲冷凌霜 提交于 2019-12-12 10:22:19

问题


I have http template which works from Postman:

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):

raw:

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

来源:https://stackoverflow.com/questions/48230251/could-not-send-request-using-resttemplate-the-same-as-in-the-postman-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!