4

The example below uses cURL to upload image file included as a binary file.

curl -i --upload-file /path/to/image.png --header "Authorization: Token" 'https://url....' 

It works fine. I need to make this request from my Java application.

I have tried next code

URL image_url = Thread.currentThread().getContextClassLoader().getResource("jobs_image.jpg");
String path = image_url.getFile();
HttpResponse<String> response = Unirest.post(uploadUrl)
  .header("cache-control", "no-cache")
  .header("X-Restli-Protocol-Version", "2.0.0")
  .header("Authorization", "Bearer " + token + "")
  .field("file", new File(path))
  .asString();

However, it returns status 400 Bad Request. Is there any way to call such request from Java?

This is a request from LinkedIn v2 API: https://learn.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context#upload-image-binary-file

  • Upvoting! I had the same problem converting this call to Ruby's Rest Client, but then I found another problem (already fixed). Haven't had the chance to go back to this one yet. https://stackoverflow.com/questions/54201879/bad-request-attempting-to-associate-a-ugc-with-an-asset-thats-not-owned-by-the – Ervin Kalemi Feb 05 '19 at 21:57
  • I've had the same problem, you can see my solution here: https://stackoverflow.com/questions/54932485/image-share-with-linkedin-v2-api-not-posting-on-page-feed/54944873#54944873 – AlexV Mar 01 '19 at 12:42

3 Answers3

3

After several hours of banging my head against the wall, I finally figured out how to convert the curl call into a RestClient one (I'm using Ruby on Rails).

I think the problem you're having is that you have to pass the MIME type as the Content-Type in the request headers.

I'm using MiniMagick to figure out the MIME type of the image I'm uploading to LinkedIn. MiniMagick can also give you the binary string of the image that LinkedIn requires, so it's a win-win situation.

This is the call that finally worked:

file = MiniMagick::Image.open(FILE_PATH)
RestClient.post(UPLOAD_URL, file.to_blob, { 'Authorization': 'Bearer TOKEN', 'Content-Type': file.mime_type })
Ervin Kalemi
  • 569
  • 4
  • 14
2

Below method will upload the image to linkedIn

Reference : https://learn.microsoft.com/en-us/linkedin/marketing/integrations/community-management/shares/vector-asset-api#upload-the-image

private void uploadMedia(String uploadUrl,String accessToken) throws IOException {           
           RestTemplate restTemplate = new RestTemplate();
           HttpHeaders headers = new HttpHeaders();
           headers.add("Authorization","Bearer "+accessToken);
           byte[] fileContents = Files.readAllBytes(new 
           File("path_to_local_file").toPath());
           HttpEntity<byte[]> entity = new HttpEntity<>(fileContents, headers);
           restTemplate.exchange(uploadUrl,HttpMethod.PUT, entity, String.class);
      }
Mani
  • 604
  • 7
  • 9
0

I think the curl command curl -i --upload-file /path/to/image.png --header "Authorization: Token" 'https://url....'

uses PUT while your Java client uses POST

Source: The man page of curl.

       -T, --upload-file <file>
              This  transfers  the  specified local file to the remote URL. If
              there is no file part in the specified URL, Curl will append the
              local file name. NOTE that you must use a trailing / on the last
              directory to really prove to Curl that there is no file name  or
              curl will think that your last directory name is the remote file
              name to use. That will most likely cause the upload operation to
              fail. If this is used on an HTTP(S) server, the PUT command will
              be used.

Not sure if this is the actual problem though. Your API doc link actually specifies POST.

Dan_Maff
  • 311
  • 2
  • 7
  • I have tried both POST and PUT methods. I think the problem is in the wrong structure of my request. – Serhii Savchenko Feb 05 '19 at 19:30
  • I have the same Bad request problem making Image upload request using php curl. But when I make the curl -i --upload-file request from command line it works fine. Still looking for an answer. Did you found the way to make an upload? – Valery Patskevich Feb 13 '19 at 18:08
  • Checkout my answer below. I'm not using PHP nor Java, but I think the problem is the same for all languages. You need to pass the MIME type of the image as the Content-Type in the request headers. – Ervin Kalemi Feb 27 '19 at 10:15