正文是原始文本格式,请求是POST方法。我正在使用此代码。
@RequestMapping(value = "/run", method = RequestMethod.POST)
ResponseEntity<String> runReport(){
RestTemplate restTemplate = new RestTemplate();
String fooResourceUrl = "requestURL";
String auth = "username:password";
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")) );
String authHeader = "Basic " + new String( encodedAuth );
// Defining the Headers
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
headers.add("Authorization", authHeader);
headers.add("Content-Type", "multipart/form-data; boundary=\"Boundary_1_1153447573_1465550731355\"");
headers.add("Accept", "multipart/form-data");
// Defining the Body
String outputFormatType = "pdf";
String body = "--Boundary_1_1153447573_1465550731355\n"
+"Content-Type: application/json\n"
+"Content-Disposition: form-data;" + " "+"name=\"ReportRequest\"\n\n"
+"{\"byPassCache\":true,\"flattenXML\":false,\"attributeFormat\":"+ "\""+outputFormatType+"\"" +"}\n"
+"--Boundary_1_1153447573_1465550731355--";
HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("username", "password"));
ResponseEntity<String> result = restTemplate.exchange(fooResourceUrl, HttpMethod.POST, entity, String.class);
return result;
}
答案 0 :(得分:0)
<强>解决:: 强> 删除行: restTemplate.getInterceptors()。add(new BasicAuthorizationInterceptor(“username”,“password”)); 因为它已经添加到标题中。并将body参数添加到HttpEntity: HttpEntity entity = new HttpEntity(body,headers);