Send array in JSON POST request with HTTParty

3,473 views
Skip to first unread message

Ruben Martinez Jr.

unread,
Aug 13, 2014, 4:03:13 PM8/13/14
to httpar...@googlegroups.com

Cross-posted from SO. I'm trying to work with a 3rd party API that requires an array to be sent within a POST request body. I've already gotten the hang of sending JSON; I've read you just need to set some headers and call to_json on the POST body. However, I'm not sure how to embed an array within that POST body. I've tried the following:

HTTParty.post(url, 
    :body => { 
        :things => [{myid: 1}, {id: 2}, {id: 3}],
    }.to_json,
    :headers => { 
        'Content-Type' => 'application/json', 
        'Accept' => 'application/json'
    }
)

but this is giving me a server error, leading me to believe the array isn't being formatted correctly. Could someone please advise on how to send an array within a JSON POST request? Thanks!

Ruben Martinez Jr.

unread,
Aug 13, 2014, 7:07:10 PM8/13/14
to httpar...@googlegroups.com
Please ignore above, revised:

Cross-posted from SO. I'm trying to work with a 3rd party API that requires an array to be sent within a POST request body. I've already gotten the hang of sending JSON; I've read you just need to set some headers and call to_json on the POST body. However, I'm not sure how to embed an array within that POST body. I've tried the following:

HTTParty.post(url, 
    :body => { 
        :things => [{:myid => 1}, {:id => 2}, {:id => 3}],
    }.to_json,
    :headers => { 
        'Content-Type' => 'application/json', 
        'Accept' => 'application/json'
    }
)

but this is giving me a server error, leading me to believe the array isn't being formatted correctly. Could someone please advise on how to send an array within a JSON POST request? Thanks!

The error I get back is the following:
#<HTTParty::Response:0x10 parsed_response=nil, 
@response=#<Net::HTTPInternalServerError 500 Internal Server Error readbody=true>, 
@headers={"error_message"=>["Can not deserialize instance of java.lang.Long out of 
START_OBJECT token  at [Source: org.apache.catalina.connector.CoyoteInputStream@30edd11c; 
line: 1, column: 15] (through reference chain: REDACTED[\"things\"])"], 
"error_code"=>["0"], "content-length"=>["0"], 
"date"=>["Wed, 13 Aug 2014 22:53:49 GMT"], "connection"=>["close"]}> 

The JSON should be in the format:

{ "things" : [ {"id": "..."}, {"id: "..."}, ... ] } 

ravishankar mishra

unread,
Mar 22, 2017, 3:17:41 PM3/22/17
to HTTParty Gem

Use this

@responces = HTTParty.post("https://whitelabel-staging.aclipsa.com/api_v2/store/purchases",
                   {
                    :body => [ { "product_id" => "#{sub.product_id}", "store_uid" => "#{sub.id}" } ].to_json,
                    :headers => { 'Content-Type' => 'application/json', 'Token' => 'request.headers["HTTP_TOKEN"]'}
                    })
Reply all
Reply to author
Forward
0 new messages