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

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

问题描述

我确定我完全打扰了这一点,但我得到了这个远的同伙Stack Overflow用户的帮助,所以感谢到此为止。



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



我也不想使用任何类型的代理



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

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



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

 < 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>

同样,我肯定会弄乱我的数据结构,但我不知道什么我错了,特别是正确设置字段:{first_name:myFirstName} 结构/数组。

解决方案

您应该将请求字符串作为httpparam类型的主体发送。请求的主体可以是类似于您准备的结构的整个表单范围。确保在隐式结构创建期间使用数组符号设置结构键或将其置于引号中,以确保在serializeJSON()发生时保持其正确的外壳,否则ColdFusion将大写结构键。

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

< cfhttp url =http://api.url.commethod =postresult =httpResptimeout =60>
< cfhttpparam type =headername =Content-Typevalue =application / json/>
< cfhttpparam type =bodyvalue =#serializeJSON(stFields)#>
< / cfhttp>

更新10/26/13

所有的工作,我一直在做最近与API我认为我会更新一个简单的方法来自动化这个套管,我发现。我使用了 JSON Util 库和Ben Nadel的 JSON序列化程序实用程序CFC 为所有人实现更好的序列化一致性返回。



下面是我如何实现这个示例GIST。

https://gist.github.com/timmaybrown/7226809



当我转换到在我的项目中使用持久化实体CFC,我发现使用我自己的子CFC方法扩展Ben Nadel的序列化程序CFC,使用 getComponentMetaData()函数来构建不同键的结构以及用于序列化的外壳。该方法允许我的api自动继承我的属性名称在我的实体内的套,是非常有用的。



更新9/8/16
Re:我上面提到的一致套管。我已经趋向于一个不同的列命名约定在我的数据库中的较新的项目,所以我不必打架很多这些问题。 first_name 而不是 firstName 等。


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.

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.

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

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"
}

And this is what I've built thus far but continue to receive "unable to parse JSON" errors from the remote API:

<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>

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.

解决方案

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>

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.

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

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.

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天全站免登陆