我在使用API OpenPay.mx时遇到了一些问题(支付墨西哥银行及其他支付服务)。此API返回JSON。我尝试使用CFHTTP访问API,但它返回HTTP 400不正确的请求错误。
完成错误:
https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges {"类别":"请求","说明":"意外的字符(' m'(代码 109)):期望一个有效值(数字,字符串,数组,对象,'真', '假'要么 '空')"" HTTP_CODE" 400" ERROR_CODE":1001," REQUEST_ID":" 79a19194 -61a2-49e5-8cbc-c83f5c93ce69"}
在关于错误1001-400的API帮助的错误列表中,这是解释:
请求格式为JSON,字段格式不正确, 或者请求没有所需的字段。
ColdFusion代码:
<cfset request_id = "sk_722a9645ea0040899ccd1f0a53dfcf53">
<cfset method="store">
<cfset amount=100>
<cfset description="Cargo con tienda">
<cfset customer="Gabriel Villafuerte">
<cfhttp url="https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges"
method="post" charset="utf-8" username="#request_id#" password=""
throwonerror="no">
<cfhttpparam type="header" name="Content-Type" value="application/json"/>
<cfhttpparam name="method" type="formfield" value="#method#">
<cfhttpparam name="amount" type="FormField" value="#amount#">
<cfhttpparam name="description" type="FormField" value="#description#">
<cfhttpparam name="customer" type="FormField" value="#customer#">
</cfhttp>
<CFDUMP var="#cfhttp#">
<!---display results--->
<cfoutput>
HTTP Response = #cfhttp.statusCode# <br>
<textarea cols=80 rows=10>
https://sandbox-api.openpay.mx/v1/maidzkihk7utcvzhucwk/charges
#cfhttp.fileContent#
</textarea>
</cfoutput>
有没有人知道如何给出正确的字段格式?
答案 0 :(得分:2)
我不熟悉API,但查看文档和示例,我怀疑错误消息的确意味着它所说的内容(强调我的)。
请求的格式不是JSON ,字段没有 格式正确,或者请求没有所需的字段。
请求值必须作为JSON提交,而不是单独的字段,显然必须包含所有必需的值。而不是使用“formfield”,将值放在结构中。使用serializeJSON()将其转换为JSON。然后使用参数类型“body”将JSON传递给API。
您需要查看API示例,无论您调用哪种方法,都需要确定哪些参数是必需的。但是,Charges via Store Example稍作修改:
通过商店示例收费
<!--- sample request from API --->
<!--- note: increased "order_id" value by arbitrary amount --->
<cfset timeNow = now()>
<cfset requestData = {
"method" : "store",
"amount" : 100,
"description" : "Cargo con tienda",
"order_id" : "oid-00100",
"due_date" : dateFormat(timeNow, "yyyy-mm-dd")&"T"&timeFormat(timeNow, "HH:nn:ss")
}>
<cfhttp url="https://sandbox-api.openpay.mx/v1/mzdtln0bmtms6o3kck8f/customers/ag4nktpdzebjiye1tlze/charges"
method="post"
charset="utf-8"
username="sk_e568c42a6c384b7ab02cd47d2e407cab:"
password=""
throwonerror="no">
<cfhttpparam type="header" name="Content-Type" value="application/json"/>
<cfhttpparam type="body" value="#serializeJSON(requestData)#">
</cfhttp>
<cfdump var="#cfhttp#">
<强>结果:强>