0

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]
halfer
  • 19,824
  • 17
  • 99
  • 186
clint
  • 1,786
  • 4
  • 34
  • 60
  • you can use retrofit to do so Mentioned [here](https://stackoverflow.com/questions/39953457/how-to-upload-image-file-in-retrofit-2/56412584) – Mouamle Hasan Feb 05 '20 at 08:16
  • Check my answer here : https://stackoverflow.com/questions/53854400/how-to-run-a-curl-command-from-java/53854617#53854617 – Vinay Hegde Feb 05 '20 at 13:06

0 Answers0