为cfhttp添加正文的正确方式



我想用lucee/coldfusion做一个API请求。

我这样设置我的令牌请求:

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]");
};

但是错误信息告诉我"grant_type"需要包含在内,所以看起来我的身体在这里没有正确发送。

有人能帮我一下吗?编辑:

我也试过这个:

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()#");
};

您的第二次尝试是正确的方法,但您需要添加一个Content-Type标头,将正文指定为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()#");
}

显然,body JSON也需要匹配API所期望的。

最新更新