如何使用ColdFusion CFHTTP连接到ShiftPlanning.com API

时间:2016-02-11 00:33:00

标签: json api coldfusion

用于身份验证的ShiftPlanning API文档(https://www.shiftplanning.com/api/authentication/)要求以JSON格式发送POST请求,如下所示:

{
  "key": "YOUR_API_KEY",
  "request": {
    "module": "staff.login",
    "method": "GET",
    "username": "xxxxxxx",
    "password": "xxxxxxx"
  }
}

看起来非常简单,但我似乎并没有连接到API。当我CFDUMP CFHTTP响应时,我得到的是结构的filecontent值中的页面HTML源,没有JSON。

这是我用于CFHTTP呼叫的代码:

<!--- set values for API authentication --->
<cfset stFields = {
    "key" = "xxxxxxxxxxxxxxxxxxxxxxxx",
    "request" = {
        "module" = "staff.login",
        "method" = "GET",
        "username" = "xxxxx",
        "password" = "xxxxx"
    }
}>

<!--- Send an authentication request to SHIFTPLANNING API --->
<cfhttp method="post" url="https://www.shiftplanning.com/api/" timeout="30">
    <cfhttpparam type="header" name="Accept" value="application/json" />
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#serializeJSON(stFields)#" />
</cfhttp>

当我CFDUMP serializeJSON(stFields)时,我得到:

{
 "request": {
   "username":"xxxxx",
   "module":"staff.login",
   "method":"GET",
   "password":"xxxxx"
  },
"key":"xxxxxxxxxxxxxxxxxxxxxxxx"
}

我错过了一些明显的东西吗?

1 个答案:

答案 0 :(得分:0)

Using Chrome dev tools on the api explorer, it seems they are passing in the JSON data in a form field called data. Changing your cfhttp call to this returned JSON data for me:

<!--- Send an authentication request to SHIFTPLANNING API --->
<cfhttp method="post" url="https://www.shiftplanning.com/api/" timeout="30" charset="utf-8">
    <cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded">
    <cfhttpparam type="formfield" name="data" value="#serializeJSON(stFields)#" />
</cfhttp>