HTTParty POST 调用返回无效 JSON

HTTParty POST call returning invalid JSON

我希望 post 发送电子邮件至 API,但我无法得到可行的回复。当我在终端中简单地卷曲 API 时,数据按预期返回。

@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'}
      )

上面returns这个错误

<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"]}>

我可以用 .to_json 方法包装整个调用,这允许调用成功,但我无法使用标准 @omnics.body 方法对响应做任何事情。当我将所有内容包装在 .to_json 方法中时,这是我得到的结果:

 => "{\"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\"]}"

我已经尝试将 to_json 方法添加到 header 部分和 body 部分,建议 here 如下所示: :body => {'email' => 'me@email.com'}.to_json 而不是包装整个函数,但也会出错。

希望能有任何想法,因为我只是一个最有兴趣的开发人员,可能忽略了一些显而易见的事情。

终于成功了。我必须将正文值包含在一个数组中。语法如下:

@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
    }