如何修复这400 Bad状态错误?

时间:2016-10-18 11:57:06

标签: json coldfusion coldfusion-11

下图显示错误和cfdump输出

enter image description here

这是我的JSON响应:

{
  "properties": [{
    "property": "email",
    "value": "testingapis@hubspot.com"
  }, {
    "property": "firstname",
    "value": "Adrian"
  }, {
    "property": "lastname",
    "value": "Mott"
  }, {
    "property": "website",
    "value": "http://hubspot.com"
  }, {
    "property": "company",
    "value": "HubSpot"
  }, {
    "property": "phone",
    "value": "555-122-2323"
  }, {
    "property": "address",
    "value": "25 First Street"
  }, {
    "property": "city",
    "value": "Cambridge"
  }, {
    "property": "state",
    "value": "MA"
  }, {
    "property": "zip",
    "value": "02139"
  }]
}

这是我正在使用的代码。我使用cfset设置值,然后使用cfhttp

发布数据
<cfset stFields='{
           {
            "properties": [
                {
                    "property": "email",
                    "value": "testingapis@hubspot.com"
                },
                {
                    "property": "firstname",
                    "value": "Adrian"
                },
                {
                    "property": "lastname",
                    "value": "Mott"
                },
                {
                    "property": "website",
                    "value": "http://hubspot.com"
                },
                {
                    "property": "company",
                    "value": "HubSpot"
                },
                {
                    "property": "phone",
                    "value": "555-122-2323"
                },
                {
                    "property": "address",
                    "value": "25 First Street"
                },
                {
                    "property": "city",
                    "value": "Cambridge"
                },
                {
                    "property": "state",
                    "value": "MA"
                },
                {
                    "property": "zip",
                    "value": "02139"
                }
            ]
        }
    }'>

  <cfhttp url="https://api.hubapi.com/contacts/v1/contact/?hapikey=demo" method="post" result="httpResp" timeout="60">
    <cfhttpparam type="header" name="Content-Type" value="application/json" />
    <cfhttpparam type="body" value="#serializeJSON(stFields)#">
  </cfhttp>
<cfdump var="#httpResp#" label="HTTP response">

转储后,会显示错误400 Bad request Access 请帮我纠正这个错误。

1 个答案:

答案 0 :(得分:0)

创建结构而不是字符串。使用CFDump查看它是否正确。像这样:

<cfset stFields = {
    "properties": [
        {
            "property": "email",
            "value": "testingapis@hubspot.com"
        },
        {
            "property": "firstname",
            "value": "Adrian"
        },
        {
            "property": "lastname",
            "value": "Mott"
        },
        {
            "property": "website",
            "value": "http://hubspot.com"
        },
        {
            "property": "company",
            "value": "HubSpot"
        },
        {
            "property": "phone",
            "value": "555-122-2323"
        },
        {
            "property": "address",
            "value": "25 First Street"
        },
        {
            "property": "city",
            "value": "Cambridge"
        },
        {
            "property": "state",
            "value": "MA"
        },
        {
            "property": "zip",
            "value": "02139"
        }
    ]
}>
<cfdump var="#stfields#">

然后在cfhttp中使用该结构的序列化版本。您也可以将其作为测试进行CFDump。

<cfdump var="#serializeJSON(stfields)#">