I am looking for Java equivalent of following curl command:
curl -X POST -H "Authorization: hbapi:abcd..." --form "type=image" --form "file=@/Folder1/IMG_0332.JPG" "https://api.xyz.com/creative-upload?member_id=123"
I tried following:
public static void main(String[] args) {
try {
RestTemplate restTemplate = new RestTemplate();
String url = "https://api.xyz.com/creative-upload?member_id=123";
HttpHeaders headers = new HttpHeaders();
headers.set("accept", "application/json");
headers.set("Authorization", "authn:abcd..");
headers.setContentType(MediaType.IMAGE_JPEG);
MultiValueMap<String, Object> entity = new LinkedMultiValueMap<>();
File file = new File("C:/users/Admin/workspace/abcd/src/main/java/testAPI/Team-India.jpeg");
entity.add("file", new FileSystemResource(file));
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(entity, headers);
ResponseEntity<String> response = restTemplate.postForEntity(url, requestEntity, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
System.out.println("success=" + response.toString());
}
else {
System.out.println("failure:" + response.toString());
}
System.out.print("response TestXandrPOSTFile:" + response.toString());
}
catch (Exception e) {
System.out.print("Error: in TestRESTPostCustomActivity, the error is " + e);
}
}
But, it gives following error:
the error is org.springframework.web.client.RestClientException: Could not write request: no suitable HttpMessageConverter found for request type [org.springframework.util.LinkedMultiValueMap] and content type [image/jpeg]