Greg Seedsman
Greg Seedsman

Reputation: 21

CFHTTP and the differences in OAuth2.0 POST login methods

I'm constantly doing login POSTs to various APIs using Coldfusion 2016. No matter how specific the API documentation seems to be (with CURL and HTTPie examples) it always seems to be a trial and error process to work out how to pass the required variable using CFHTTP.

Are there any shortcuts I can use to know which CFHTTP method to use first time? OS, age of API, HEAD requests, etc.

Eg. A login designated as OAuth2.0 can require either...

<!--- "form" variables posted as a URL encoded string in the body --->
<cfhttp method="POST" url="https://some.important.api/oauth/oauth20/token">
   <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
   <cfhttpparam type="body" value="grant_type=client_credentials&client_id=#ID#&client_secret=#secret#" />
</cfhttp>

<!--- "form" variables posted as JSON in the body --->
<cfhttp method="POST" url="https://some.important.api/oauth/oauth20/token">
   <cfhttpparam type="header" name="Content-Type" value="application/json" />
   <cfhttpparam type="body" 
      value="{ grant_type : 'client_credentials', client_id : '#ID#', client_secret : '#secret#' }" />
</cfhttp>

<!--- Actual form variables posted as such. This is what makes sense from CURL examples but is only correct about 1/3 of the time --->
<cfhttp method="POST" url="https://some.important.api/oauth/oauth20/token">
   <cfhttpparam type="formfield" name="grant_type" value="client_credentials" />
   <cfhttpparam type="formfield" name="client_id" value="#ID#" />
   <cfhttpparam type="formfield" name="client_secret" value="#secret#" />
</cfhttp>

Another example is passing a file using CFHTTP

<!--- Post a file using CFHTTP. Works 1/2 the time --->
<cfhttp method="POST" url="https://some.important.api/postOrder">
   <cfhttpparam type="file" name="order_file" file="#pathToCSVfile#" />
   <cfhttpparam type="formfield" name="apikey" value="#apiKey#" />
</cfhttp>

<!--- Requires completely manual method the other 1/2 of the time --->
<cfhttp method="POST" url="https://some.important.api/postOrder">
   <cfhttpparam type="header" name="content-type" value="multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" />
   <cfhttpparam type="body" value='------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="order_file"; filename="myCSVFile"
Content-Type: text/csv

#actualCSVText#
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="apikey"

#apiKey#
------WebKitFormBoundary7MA4YWxkTrZu0gW--' />
</cfhttp>

Upvotes: 1

Views: 347

Answers (0)

Related Questions