• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
1

Help Converting cURL to CFHTTP

Engaged ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

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?

TOPICS
Advanced techniques

Views

183

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Oct 12, 2023 Oct 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 typ

...

Votes

Translate

Translate
Enthusiast ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

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.)

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Oct 12, 2023 Oct 12, 2023

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation