【问题标题】:API request Coldfusion (GET request)API 请求 Coldfusion(GET 请求)
【发布时间】:2017-10-13 14:44:30
【问题描述】:

我正在尝试构建对外部 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 编码

【问题讨论】:

  • 你的代码在哪里?
  • 到目前为止我所做的只是玩弄 标签,但没有任何运气。这篇文章的目的只是帮助我构建一个有效的请求,然后可能只是将结果转储到页面上,因为我可以从那里了解如何使用该 JSON 对象。
  • 我们不能为你做这件事。您需要发布您的代码,以便我们告诉您哪里出错了。 Google cfhttp 文档将为您提供 http 请求的基础知识。
  • 您需要 cfhttpparam type="formfield" 来获取您在示例请求正文中看到的所有值:即用户名、密码和授权类型
  • 不知道别的,你可能需要DeserializeJSON() content属性的结果。

标签: api coldfusion httprequest


【解决方案1】:

要访问来自 api 请求的 JSON 响应,您需要使用 deserializeJSON(cfhttp.fileContent) 将返回一个冷融合结构。您可以使用 cfdump 进行验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-19
    • 2011-12-11
    • 1970-01-01
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多