如何使用 Coldfusion CFHTTP 将 JSON 数据发布到远程 API [英] How to POST JSON Data to Remote API Using Coldfusion CFHTTP

查看:22
本文介绍了如何使用 Coldfusion CFHTTP 将 JSON 数据发布到远程 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确信我完全搞砸了,但在 Stack Overflow 其他用户的帮助下,我走到了这一步,非常感谢.

I'm sure that I'm completely botching this up but I got this far with the help of fellow Stack Overflow users, so thanks thus far.

我需要将 JSON 数据发布到远程 API.显然由于 SOP 问题我不能使用 jQuery,并且远程 API 不支持 JSONP.

I need to POST JSON data to a remote API. Obviously I can't use jQuery due to SOP issues, and the remote API does not support JSONP.

我也不想使用任何类型的代理来绕过 SOP 限制.

I also don't want to have to use any type of proxy as to get around the SOP limitations.

根据 API 文档 (http://myemma.com/api-docs/),这是他们期望的数据格式(请求和响应数据以 JSON 格式传输):

Per the API docs (http://myemma.com/api-docs/), this is the formatting of the data they expect (request and response data is transferred as JSON):

POST https://api.e2ma.net//123/members/add
{
  "fields": {
    "first_name": "myFirstName"
  }, 
  "email": "email@domain.com"
}

这是我迄今为止构建的,但继续从远程 API 收到无法解析 JSON"错误:

<cfset fields[name_first]="#SerializeJSON( "myFirstName" )#" />
<cfset form.email="#SerializeJSON( "email@domain.com" )#" />

<cfhttp
  url="https://api.e2ma.net/123/members/add"
  method="POST"
  username="username"
  password="pssword"
  useragent="#CGI.http_user_agent#"
  result="objGet">

  <!--- add email --->
  <cfhttpparam
    type="formfield"
    name="email"
    value='#form.email#'
  />

  <!--- add field: name_first --->
  <cfhttpparam
    type="formfield"
    name="fields"
    value='#fields[name_first]#'
  />

</cfhttp>

<cfoutput>#objGet.FileContent#</cfoutput>

再一次,我肯定会以某种方式破坏我的数据结构,但我不确定我做错了什么,特别是关于正确设置 "fields": { "first_name": "myFirstName"} 结构/数组.

Again, I'm surely mangling the structure of my data somehow, but I'm not sure what I'm doing wrong, particularly regarding properly setting the "fields": { "first_name": "myFirstName" } structure/array.

推荐答案

您应该将请求字符串作为 httpparam 类型的主体发送.请求的主体可能类似于您准备好的结构的整个表单范围.请务必使用数组表示法来设置结构键或在隐式结构创建期间将它们放在引号"中,以确保在执行 serializeJSON() 时它们保持正确的大小写,否则 ColdFusion 会将结构键大写.

You should send your request string as the httpparam type of body. The body of the request could be something like the entire form scope of your prepped structure. Be sure to either use array notation for setting your structure keys or put them in "quotes" during the implicit structure creation to ensure they retain their proper casing when the serializeJSON() takes place otherwise ColdFusion will uppercase the structure keys.

<cfset stFields = {
    "fields" = {
        "first_name" = "myFirstName"
     }, 
     "email" = "email@domain.com"
}>   

<cfhttp url="http://api.url.com" method="post" result="httpResp" timeout="60">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#serializeJSON(stFields)#">
</cfhttp>

2013 年 10 月 26 日更新
对于我最近使用 API 所做的所有工作,我想我会更新一种简单的方法来自动化我发现的这种外壳.我使用了 JSON Util 库和 Ben Nadel 的 JSON Serializer Utility CFC 为所有人实现更好的序列化一致性返回.

Update 10/26/13
For all the work I've been doing lately with APIs I thought I'd update an easy way to automate this casing that I've found. I've used a combination of the JSON Util library and Ben Nadel's JSON Serializer Utility CFC to accomplish much better serialization consistency for all returns.

以下是我如何实现此功能的 GIST 示例.
https://gist.github.com/timmaybrown/7226809

Below is an example GIST of how I've implemented this.
https://gist.github.com/timmaybrown/7226809

当我在我的项目中过渡到使用持久实体 CFC 时,我发现使用我自己的子 CFC 方法扩展 Ben Nadel 的序列化程序 CFC,该方法使用 getComponentMetaData() 函数构建不同键的结构和序列化遵循的大小写.该方法允许我的 api 在我的实体中自动继承我的属性名称的大小写,并且非常有用.重新初始化会产生一些开销,但值得在 API 中保持大小写一致.

As I've transitioned to using persistent entity CFCs in my projects, I've found that extending Ben Nadel's serializer CFC with my own child CFC method that loops all my persistent cfc's properties using the getComponentMetaData() function to build a structure of distinct keys and the casing for the serialization to follow. The approach allows my api to inherit automatically the casing of my property names within my entities and is very useful. A bit of overhead on reinit, but well worth it to keep your casing consistent in your API.

2016 年 9 月 8 日更新回复:我上面关于一致外壳的观点.对于较新的项目,我倾向于在我的数据库中使用不同的列命名约定,因此我不必处理很多这些问题.first_name 而不是 firstName 等.

Update 9/8/16 Re: my point above about consistent casing. I have trended toward a different column naming convention in my databases for newer projects so I don't have to fight with a lot of these issues. first_name instead of firstName etc.

这篇关于如何使用 Coldfusion CFHTTP 将 JSON 数据发布到远程 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆