3

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.

sulleh
  • 321
  • 5
  • 14
  • Progress! `@omnics3 = HTTParty.post("https://qo.api.liveramp.com/v1/idl_resolution", options) options = { headers: { "Content-Type": "application/json", "X-Api-Key" => "key" }, query: { data: true }, body: { email: ["value"] }.to_json }` This progresses beyond the JSON error, but it looks like the api needs objects (i.e. `email_sha1`) to be nested inside an "[" object. Without it, I get the following error: `json: cannot unmarshal object into Go value of type []quickonboarding.QORecord` as indicated here https://quick-onboard.readme.io/reference – sulleh Aug 05 '19 at 18:33

1 Answers1

1

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
    }
sulleh
  • 321
  • 5
  • 14