[SalesForce] How to upload an image as a binary file via the Linkedin API v2 with a ‘POST’ request

I want to know how to make a proper 'POST' request to upload an image as a binary file to Linkedin via their API. This is what they say in their documentation (https://docs.microsoft.com/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context):

Using the uploadUrl returned from Step 1, upload your image to LinkedIn. To upload your image, send a POST request to the uploadUrl with your image included as a binary file.

They then proceed to give an example using cURL but I need to make an HTTP request from my Apex code so I need the json format of the request body.

To further illustrate my point, these are the steps to post an image in Linkedin through their API:

1) Register your image to be uploaded.

2) Upload your image to LinkedIn.

3) Create the image share.

They provide the json for steps 1 and 3 but not for 2, hence my problem.

This is the part of the code I consider relevant, should you need any other part just tell me so and I will edit my question:

        httpReq.setMethod('POST');
        httpReq.setEndpoint(uploadUrl);
        httpReq.setHeader('Authorization', 'Bearer '+linkedlnInfoListNew[0].Access_Token__c);        
        httpReq.setHeader('Content-Type', 'application/binary');             
        httpReq.setHeader('X-Restli-Protocol-Version', '2.0.0');
        requestBody = EncodingUtil.base64Encode(banner);
        httpReq.setBody(requestBody);

The uploadUrl variable is the URL provided in the response of step 1. As it is, this request produces no response body and has a status: created and status code: 201

I have tried the following:

To summarize, I just want to know what to put in the body of the 'POST' request mentioned in step 2 of the Linkedin API documentation on how to share an image.

EDIT: It would also be helpful if someone knows the answer to this problem but instead of using the binary of the image they use an url referencing the image.

EDIT 21/07/19: This is the image I am currently trying to share on Linkedin.

enter image description here

This is the updated http request with its headers:

    httpReq.setEndpoint(uploadUrl);
    httpReq.setBodyAsBlob(formBlob);        
    httpReq.setHeader('Connection', 'keep-alive');
    httpReq.setHeader('Content-Length', contentLength);
    httpReq.setHeader('Content-Type', contentType);
    httpReq.setMethod('POST');
    httpReq.setTimeout(120000);             //These are all the attributes that are set in the example.

    httpReq.setHeader('Authorization', 'Bearer '+linkedlnInfoListNew[0].Access_Token__c);            
    //httpReq.setHeader('x-li-format' , 'json');
    httpReq.setHeader('X-Restli-Protocol-Version', '2.0.0');

The formBlob variable is the blob of the image constructed using the code provided in this answer.

And this is the response given after the request above:

System.HttpResponse[Status=Request Entity Too Large, StatusCode=413]

Thank you for your time.

Best Answer

Content-Type needs to be multipart/form-data; there are also other requirements (see docs). This is difficult to do in Apex but possible. You should vote up this idea.

Related Topic