Salesforce Asked by Zufra on January 24, 2021
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.
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.
I was going to add a comment with a suggestion but it says it is too long. So my answer is not based on the fact that I got this working. It's simply an answer to your "what to put in the body of the 'POST' request mentioned in step 2" part. Will leave it to you to validate if it works.
I tried to find the HTTP request details from that curl example by appending the --trace-ascii /dev/stdout
args. It gives me this request details (I used a smaller size image):
0000: PUT /mediaUpload/C5522AQGTYER3k3ByHQ/feedshare-uploadedImage/0?c
0040: a=vector_feedshare&cn=uploads&m=AQJbrN86Zm265gAAAWemyz2pxPSgONtB
0080: iZdchrgG872QltnfYjnMdb2j3A&app=1953784&sync=0&v=beta&ut=2H-Ihpbf
00c0: XrRow1 HTTP/2
00cf: Host: api.linkedin.com
00e7: User-Agent: curl/7.54.0
0100: Accept: */*
010d: Authorization: Bearer redacted
012d: Content-Length: 28596
== Info: Connection state changed (MAX_CONCURRENT_STREAMS updated)!
=> Send data, 16384 bytes (0x4000)
0000: .PNG
0006: ......IHDR..............x......sBIT....|.d.....pHYs..........+..
0046: ....tEXtSoftware.www.inkscape.org..<... .IDATx...w..y7..s..>...
0086: #.*.It..#!......%ny.&q..I^G...Mb...0.cl.06H.M....$.z...j....{..
......
So the HTTP method that curl example uses is PUT
. That body seems to be the string representation of the binary data with the first line .PNG
denoting its file type.
Answered by Chun on January 24, 2021
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.
Answered by identigral on January 24, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP