API 请求 Coldfusion(GET 请求)

API request Coldfusion (GET request)

我正在尝试构建一个到外部 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 响应,您需要使用反序列化JSON(cfhttp.fileContent),这将 return 一个冷融合结构.你可以用cfdump来验证一下。