我有一个http模板,可以使用Postman:
要使用java代码执行相同的请求,我编写了以下代码:
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);
}
在日志中我看到:
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)
我使用fidddler来监控请求,但我只能监控邮递员的请求。
看起来像这样(标题):
生:
如何更正我的java代码以使邮件中的请求相同?
我对http属性使用与postman
相同的值