david-l
david-l

Reputation: 623

Converting cURL request command to Coldfusion (cfhttp)

I am trying to mimic the following cURL request using ColdFusion CFHTTP

curl -u myCl13nt1D:my53cR3tK3Y -X POST --data "grant_type=password&username=x&password=y" https://www.sitename.net/token
<cfhttp url="https://www.sitename.net/token" method="post" username="x" password="y">
  <cfhttpparam type="url" name="client_id"     value="myCl13nt1D" />
  <cfhttpparam type="url" name="client_secret" value="my53cR3tK3Y" />

  <!--- attempt 1 - without using cfhttp username/password attributes
  <cfhttpparam type="formfield" name="grant_type" value="password" />
  <cfhttpparam type="formfield" name="username" value="x" />
  <cfhttpparam type="formfield" name="password" value="y" /> 
  --->
  <!--- attempt 2 - without using cfhttp username/password attributes
  <cfhttpparam type="formField" name="data" value="grant_type=password&username=x&password=y" />
  --->

  <!--- attempt 3 - using cfhttp username/password attributes --->
  <cfhttpparam type="formField" name="data" value="grant_type=password" />
</cfhttp>

In command prompt, cURL request works returing expected result but using CFHTTP I get the following error (status code 401 Unauthorized )

{"error":"unauthorized_client","error_description":"That application is not registred or blocked"}

I've attempted different ways to pass the required parameters but they all return the same error.

Upvotes: 2

Views: 2520

Answers (2)

charlie arehart
charlie arehart

Reputation: 6884

Here's another solution that may help some facing this need (to convert some sample curl using --data). If this use of type="formfield does not work, consider also type="body".

I had a similar conversion need where formfield just did not work while body did. And it can be as simple to use. FWIW, in my case the --data needed to be json, as did the mimetype of the posted data, so (for those preferring tags, in keeping the theme above) it looked like:

<cfhttpparam type="body" value='{myjsondata}' >

For those who may appreciate a more specific use-case/example, my need was in fact for a call to the Cloudflare cache purge API, for clearing its cache of a given URL I had it caching. The complete example (again, offered here in tags) was:

<cfhttp url="https://api.cloudflare.com/client/v4/zones/#myclf_zone#/purge_cache" method="post">
    <cfhttpparam type="header" name="Content-Type" value="application/json" >
    <cfhttpparam type="header" name="X-Auth-Email" value="#myclf_email#" >
    <cfhttpparam type="header" name="X-Auth-Key" value="#myclf_apikey#" >
    <cfhttpparam type="body" value='{"files":["#myclf_cachedurl#"]}' >
</cfhttp>
<cfdump var="#cfhttp#">

Of course, I'd defined above that the 4 "myclf_" variables holding the 4 needed Cloudflare elements.

Upvotes: 0

Alex
Alex

Reputation: 7833

-u myCl13nt1D:my53cR3tK3Y is BasicAuth and split as username/password attributes in cfhttp. Try this instead:

<cfhttp url="https://www.sitename.net/token" method="post" username="myCl13nt1D" password="my53cR3tK3Y">
  <cfhttpparam type="formfield" name="grant_type" value="password" />
  <cfhttpparam type="formfield" name="username"   value="x"        />
  <cfhttpparam type="formfield" name="password"   value="y"        />
</cfhttp>

Looking at this request, you are authenticated using BasicAuth, and authorized with the endpoint's username/password login mechanism, most likely OAuth2.

Upvotes: 4

Related Questions