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!
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!
#<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: