Lee
Lee

Reputation: 999

Using cfhttp to post to an API

I'm playing around the Singly API at the moment, and there are ZERO ColdFusion examples (as far as I can see - and I've gone through a lot of google search results!) So, I'm trying to 'fudge' my way through it.

I've hit a stumbling block with something. I suspect it isn't specific to Singly, but I just can't figure out the syntax. I'm specifically stuck on the authorization. I've gotten as far as doing the second post back, which the docs state:

You will then make a post back to:

https://api.singly.com/oauth/access_token

With the following parameters in the body:

client_id Your Singly OAuth 2 client ID
client_secret Your Singly OAuth 2 client secret
code the code that was passed back in the URL above

Currently, I keep getting an error "no such app" - I suspect this is because I'm not sending the data correctly, because if I manually fire a request (using the same details), it works fine.

The part I'm stuck with is how I go about formatting everything, presumably into a cfhttpparam with a type body. I'm aware I can only use body type once in a cfhttp call. There's no indication that the data needs to be sent as JSON etc

Many Thanks

Upvotes: 3

Views: 640

Answers (1)

Lee
Lee

Reputation: 999

For the benefit of anyone else searching, Matt Busche suggested sending them as headers. That didn't work, but did point me to try sending them as formFields, which DID work. Here's the working code:

<cfhttp method="POST" url="https://api.singly.com/oauth/access_token">
    <cfhttpparam type="formField" name="client_id" value="my_client_id">
    <cfhttpparam type="formField" name="client_secret" value="my_client_secret">
    <cfhttpparam type="formField" name="code" value="#url.code#">
</cfhttp>

Upvotes: 3

Related Questions