简体   繁体   English

在httparty中构造一个json请求

[英]Constructing a json request in httparty

I am trying to replicate the following curl request using Httparty with no success. 我试图使用Httparty复制以下curl请求,但未成功。 I was wondering how best to construct the nested json with httparty. 我想知道如何最好地使用httparty构建嵌套的json。 Any examples of nest queries I have seen have been no help. 我见过的任何嵌套查询示例都没有帮助。 I am receiving a 500 error from the API I am trying to access. 我从尝试访问的API收到500错误。 Any help appreciated 任何帮助表示赞赏

curl https://api.relateiq.com/v2/contacts
-X POST
-u [API Key]:[API Secret]
-H 'Content-Type: application/json'
-H 'Accept: application/json'
-d '{
    "properties" : {
        "name" : [
            {
                "value" : "James McSales"
            }
        ],
        "email" : [
            {
                "value" : "james.mcsales@relateiq.com"
            },
            {
                "value" : "jimmy@personal.com"
            }
        ],
        "phone" : [
            {
                "value" : "(888) 555-1234"
            },
            {
                "value" : "(888) 555-0000"
            }
        ],
        "address": [
            {
                "value": "123 Main St, USA"
            }
        ],
        "company": [
            {
                "value": "RelateIQ"
            }
        ],
        "title": [
            {
                "value": "Noob"
            }
        ]
    }
}'

This is what a shortened version my httparty request looks like, 这是我的httparty请求的简化版本,

HTTParty.post(
    "https://api.relateiq.com/v2/contacts",
:body => {
        :properties => {
    :name => [{:value => "Falcao Test"}],
    :email => [{:value => "FalcaoTest@monaco.fr"}]
    }
    },
    :basic_auth => auth,
:options => { :headers => { 'Content-Type' => 'application/json' } }
)

and the request is like so 和请求是这样的

https://api.relateiq.com/v2/contacts>, @options={:limit=>5, :assume_utf16_is_big_endian=>true, :default_params=>{}, :follow_redirects=>true, :parser=>HTTParty::Parser, :connection_adapter=>HTTParty::ConnectionAdapter, :body=>{:properties=>{:name=>[{:value=>"Falcao Test"}], :email=>[{:value=>"FalcaoTest@monaco.fr"}]}}, :basic_auth=>{:username=>"5400a0b7e4b052e888539fb7", :password=>"WNyLKfLZ9wK1hEyY6249VQKQ4zS"}, :options=>{:headers=>{"Content-Type"=>"application/json"}}}, @last_uri=#https://api.relateiq.com/v2/contacts>, @raw_request=#, @last_response=#> https://api.relateiq.com/v2/contacts>,@options = {:limit => 5,:assume_utf16_is_big_endian => true,:default_params => {},:follow_redirects => true,:parser => HTTParty: :parser,:connection_adapter => HTTParty :: ConnectionAdapter,:body => {:properties => {:name => [{:value =>“ Falcao Test”}],:email => [{:value =>“ FalcaoTest@monaco.fr“}]}},:basic_auth => {:username =>” 5400a0b7e4b052e888539fb7“,:password =>” WNyLKfLZ9wK1hEyY6249VQKQ4zS“},:options => {:headers = >>”“- “ application / json”}}},@ last_uri =#https://api.relateiq.com/v2/contacts>,@ raw_request =#,@last_response =#>

I have used httparty before but not with a structure which contains nesting with [] before. 我之前使用过httparty,但之前没有使用包含[]嵌套的结构。 That is what is confusing me 那就是让我困惑的地方

Your json is different. 您的json不同。

Your curl example does 您的curl示例确实

    "name" : [
        {
            "value" : "James McSales"
        }
    ],

ie the value for the key "name" is an array containing one object (with key value) 即,键“名称”的值是一个包含一个对象(带有键值)的数组

Your httparty code has 您的httparty代码具有

 :name => {:value => "Falcao Test"}

so the value for the key "name" is not an array any more 因此键“名称”的值不再是数组

To match the original example it should be 为了匹配原始示例,它应该是

 :name => [{:value => "Falcao Test}]

I could never get this structure replicated with HTTParty, that has never happened before. 我永远无法用HTTParty复制这种结构,这是以前从未发生过的。 I ended up having to use curb and an escaped json string 我最终不得不使用遏制和转义的json字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM