我想使用我的电子邮件地址向API发送请求,但是我无法获得可用的响应。当我在终端中简单地使用cURL访问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'}
)
上面返回了这个错误
<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
方法到头部和正文部分,如这里所推荐的那样:
: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" => "key" }, query: { data: true }, body: { email: ["value"] }.to_json }
这超越了JSON错误,但是看起来API需要将对象(即email_sha1
)嵌套在“ [”对象中。如果没有它,我会收到以下错误:json:无法将对象取消编组为类型[] quickonboarding.QORecord
如此指示 https://quick-onboard.readme.io/reference - sulleh