API请求Coldfusion(GET请求)

时间:2017-10-13 14:44:30

标签: api coldfusion httprequest

我正在尝试为外部API构建HTTP请求以获取一些数据。实际上,第一个请求将是获取授权令牌,该令牌将在第二个HTTP请求上使用以获取所需的数据。 我没有运气就尝试了eveything,这是第一次构建API请求。 我得到了这个示例请求:

POST https://api2-test.com/Identity/v1/Token HTTP/1.1
Host: api2-test.com 
Accept: application/json 
Content-Type: application/x-www-form-urlencoded

userName=my.account%40mydomain.com&password=MyPassword&grant_type=password 

我正在使用ColdFusion来构建这个,任何帮助都会非常感激。

修改 这就是我一直在尝试但它无法正常工作:

<cfhttp url="https://api2-test.com/Identity/v1/Token HTTP/1.1" password="test" username="test2" result="object">
    <cfhttpparam type="header" name="Content-Type" value="application/json">
</cfhttp>

编辑2:我能够成功从API获取数据,但无法使用结果。它说它不是JSON对象!

<cfhttp url="#apiURL#" method="get" result="httpResp" timeout="120">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="formfield" name="method" value="test">
</cfhttp>
[![screen shot of the results][1]][1]<cfdump var="#httpResp#" />

我不知道为什么图片显得很奇怪,但这里是文件内容:

{"access_token":"<TokenIsHere>","token_type":"bearer","expires_in":259199,"userName":"my.account@mydomain.com","issued":"Tue, 23 May 2017 00:53:39 AD5T","expires":"Fri, 26 May 2017 00:53:39 AD5T"}

它还说:wddx编码

1 个答案:

答案 0 :(得分:1)

要从api请求访问JSON响应,您需要使用deserializeJSON(cfhttp.fileContent),它将返回coldfusion结构。您可以使用cfdump进行验证。