2017-10-13 186 views
-2

我想构建一个HTTP请求到外部API来获取一些数据。实际上,第一个请求是获取授权令牌,该授权令牌将用于第二个HTTP请求以获取所需的数据。 我已经尝试eveything没有任何运气,这是第一次建立一个API的请求。 我得到了这个例子的要求:API请求Coldfusion(GET请求)

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#" /> 

我不知道为什么图片显示奇怪,但这里是filecontent:

{"access_token":"<TokenIsHere>","token_type":"bearer","expires_in":259199,"userName":"[email protected]","issued":"Tue, 23 May 2017 00:53:39 AD5T","expires":"Fri, 26 May 2017 00:53:39 AD5T"} 

,它也说:WDDX编码

+0

你的代码在哪里? – haxtbh

+0

我到目前为止所做的只是玩标签,但没有任何运气。这篇文章的目的只是为了帮助我构建一个有效的请求,然后可能只是将结果转储到页面上,因为我可以从那里获取如何使用该JSON对象。 – DoArNa

+0

我们不能为你做。您需要发布您的代码,以便我们可以告诉您哪里出错。 Google cfhttp文档将为您提供http请求的基础知识。 – haxtbh

回答

1

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