API 请求 Coldfusion(GET 请求)



我正在尝试构建对外部API的HTTP请求以获取一些数据。实际上,第一个请求将获取一个授权令牌,该令牌将用于第二个HTTP请求以获取所需的数据。我已经尝试过没有任何运气的事情,这是第一次构建对 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 编码

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

最新更新