I am looking to post to an API with my email address, but I cannot get a workable response. When I simply cURL the API in the terminal, the data is returned as expected.
@omnics2 = HTTParty.post('https://qo.api.liveramp.com/v1/idl_resolution',
:headers => {
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Api-Key' => 'my api key',
'Accept-Encoding' => 'gzip, deflate',
'Content-Length' => '59148'
},
:body => {'email' => 'me@email.com'}
)
The above returns this error
<HTTParty::Response:0x7ff5b0d49ab0 parsed_response="Invalid JSON in request: invalid character 'e' looking for beginning of value\n", @response=#<Net::HTTPBadRequest 400 Bad Request readbody=true>, @headers={"content-type"=>["text/plain; charset=utf-8"], "x-content-type-options"=>["nosniff"], "date"=>["Fri, 02 Aug 2019 21:41:58 GMT"], "content-length"=>["78"], "via"=>["1.1 google"], "alt-svc"=>["clear"], "connection"=>["close"]}>
I can wrap the entire call with a .to_json
method, which allows for a succesful call, I just can't do anything with the response using the standard @omnics.body
method. When I wrap everything in a .to_json
method, this is the result I get:
=> "{\"content-type\":[\"text/plain; charset=utf-8\"],\"x-content-type-options\":[\"nosniff\"],\"date\":[\"Fri, 02 Aug 2019 21:45:32 GMT\"],\"content-length\":[\"78\"],\"via\":[\"1.1 google\"],\"alt-svc\":[\"clear\"],\"connection\":[\"close\"]}"
I've tried adding to_json
methods to the header section and body sections, as recommended here like so:
:body => {'email' => 'me@email.com'}.to_json
instead of wrapping the entire function, but that also errors out.
Would love any thoughts here as I am merely a hobbiest developer and am probably overlooking something obvious.
Finally got this to work. I had to make the body values enclosed within an array. Here's the syntax:
@omnics3 = HTTParty.post("https://qo.api.liveramp.com/v1/idl_resolution", options)
options = {
headers: {
"Content-Type": "application/json",
"X-Api-Key" => "my api key"
},
:debug_output => STDOUT,
query: { data: true },
body: [
{ email: ["anemail@address.com"] },
{ email: ["joe@gmail.com"] },
].to_json
}