4

I want to make an API request using lucee/coldfusion.

I setup my token request like this:

cfhttp(
    url="[myurl]"
    method="POST"
    result="token"          
) {
    cfhttpparam(type="header" name="host" value="[url]");
    cfhttpparam(type="body" name="client_id" value="[id]");
    cfhttpparam(type="body" name="client_secret" value="[secret]");
    cfhttpparam(type="body" name="grant_type" value="[credentials]");
    cfhttpparam(type="body" name="scope" value="[url]");
};

But the error message tells me that "grant_type" needs to be included, so it seems like my body here is not sent properly.

Can someone help me out?

Edit:

I also tried this:

var body = {
    "host": "[url]",
    "client_id": "[id]",
    "client_secret": "[secret]",
    "grant_type": "[credentials]",
    "scope": "[url]"
}

// Token
cfhttp(
    url="[url]" 
    method="POST"
    result="token"          
) {
    cfhttpparam(type="header" name="host" value="[url]");
    cfhttpparam(type="body" value="#body.toJson()#");
};

1 Answers1

5

Your second attempt is the right way to do it, but you need to add a Content-Type header specifying the body as JSON:

body = {
    "host": "[url]",
    "client_id": "[id]",
    "client_secret": "[secret]",
    "grant_type": "[credentials]",
    "scope": "[url]"
}
// Token
cfhttp(
    url="[url]" 
    method="POST"
    result="token"          
) {
    cfhttpparam(type="header", name="host", value="[url]");
    cfhttpparam(type="header", name="Content-Type", value="application/json");
    cfhttpparam(type="body", value="#body.toJson()#");
}

Obviously the body JSON also needs to match whatever the API is expecting.

CfSimplicity
  • 2,338
  • 15
  • 17