简体   繁体   中英

Constructing a json request in httparty

I am trying to replicate the following curl request using Httparty with no success. I was wondering how best to construct the nested json with httparty. 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. 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.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=#>

I have used httparty before but not with a structure which contains nesting with [] before. That is what is confusing me

Your json is different.

Your curl example does

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

ie the value for the key "name" is an array containing one object (with key value)

Your httparty code has

 :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. I ended up having to use curb and an escaped json string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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