Skip to main content
Legend
October 12, 2023
Answered

Help Converting cURL to CFHTTP

  • October 12, 2023
  • 3 replies
  • 905 views

I'm hoping I'm missing something obvious here and this will be a simple fix.

 

I'm working with the LinkedIn API, specifically creating a share.

 

I've got all the authentication working and have successfully created plain text and article shares.  However, in order to include an image with a share I'm hitting a snag.

 

According to the documentation, after making an initial call to obtain the uploadUrl, you then upload the file.  Specifically:  "To upload your image, send a POST request to the uploadUrl with your image included as a binary file."

 

It gives this cURL example:

 

curl -i --upload-file /Users/peter/Desktop/superneatimage.png --header "Authorization: Bearer redacted" 'https://api.linkedin.com/mediaUpload/blahblahblah'

 

 

I have tried numerous ways to replicate this call with CFHTTP, but LinkedIn always returns a 400 Bad Request response.  No other info.

 

Here's my latest try:

 

<cfhttp url="#uploadURL#" method="post" result="uploadRes" multipart="true" multipartType="form-data">
   <cfhttpparam type="header" name="Authorization" value="Bearer #accessToken#">
   <cfhttpparam type="header" name="X-Restli-Protocol-Version" value="2.0.0">
   <cfhttpparam type="file" file="#pathtoPNGfile#" name="imgFile" mimetype="image/png">
</cfhttp> 

 

 

I have been able to assemble a cURL request like the example and it works fine.  Returns a 201 Created response.  Using the same data in CFHTTP gives the 400 response.

 

I am by no means a cURL expert so my question is:

  • --upload-file, I'm unclear how this is working in cURL. 
  • is it the same as CFHTTPPARAM type=File?
  • should I be converting the image to binary and send through the body?

 

Ideas?

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer James Moberg

If you want to see what CURL is doing, use the command line and pass the verbose flag (-v). This will provide more information regarding the full transaction that CURL is performing behind the scenes.

 

Deepgram (audio transcription service) has a similar binary upload requirement in their API documentation for posting audio files. Their CURL instructions were to use "--data-binary @youraudio.wav".  In order to do that with CFHTTP, I had to use the fileReadBinary function, pass the file's mime type in a "Content-Type" header and then pass the file's binary data as the request "body".

Here's the sample CFML code that I used to upload a binary file to Deepgram's API.  I wonder if this approach will work with your LinkedIn upload issue.
https://gist.github.com/JamoCA/c7cdeaf9842c6233fe8e55b0c37642d5

(NOTE: CFML code shared via Github gist so that it's not deleted/munged by Adobe or liable to their terms.)

 

 

3 replies

James Moberg
James MobergCorrect answer
Inspiring
October 12, 2023

If you want to see what CURL is doing, use the command line and pass the verbose flag (-v). This will provide more information regarding the full transaction that CURL is performing behind the scenes.

 

Deepgram (audio transcription service) has a similar binary upload requirement in their API documentation for posting audio files. Their CURL instructions were to use "--data-binary @youraudio.wav".  In order to do that with CFHTTP, I had to use the fileReadBinary function, pass the file's mime type in a "Content-Type" header and then pass the file's binary data as the request "body".

Here's the sample CFML code that I used to upload a binary file to Deepgram's API.  I wonder if this approach will work with your LinkedIn upload issue.
https://gist.github.com/JamoCA/c7cdeaf9842c6233fe8e55b0c37642d5

(NOTE: CFML code shared via Github gist so that it's not deleted/munged by Adobe or liable to their terms.)

 

 

Legend
October 12, 2023

Thanks so much.  For reference, final code that successfully loaded the image as a binary file:


<cfset binImage = fileReadBinary(getTempDirectory() & imgName)>

<cfhttp url="#uploadURL#" method="post" result="uploadRes">
    <cfhttpparam type="header" name="Authorization" value="Bearer #accessToken#">
    <cfhttpparam type="header" name="X-Restli-Protocol-Version" value="2.0.0">
    <cfhttpparam type="header" name="Content-Type" value="#fileGetMimeType(getTempDirectory() & imgName)#">
    <cfhttpparam type="body" value="#binImage#">
</cfhttp>

James Moberg
Inspiring
October 12, 2023

I actually tackled this issue two days prior to your post. I searched and couldn't find any CFML demo that performed this this type of API file upload. Every code demo performed a regular file upload. I'm glad that this approach worked.