我有以下代码:
require 'Typhoeus'
url = Typhoeus::Request.new("https://fluidsurveys.com/api/v2/webhooks/subscribe/",
userpwd: "username_test:password_test",
method: :post,
body: {
'subscription_url' => 'http://glacial-spire-test.herokuapp.com/hooks/response_created_callback',
'event' => 'response_complete'
},
headers: { 'Content-Type' => "application/json"})
response = url.run.body
puts response
这将返回响应代码400
和错误:
Content-Type set to application/json but could not decode valid JSON in request body.
以下是我正在进行的API调用的文档:http://docs.fluidsurveys.com/api/webhooks.html
POST /api/v2/webhooks/subscribe/¶
Returns a status of 409 if a webhook with the subscription url already exists. Returns a
status of 201 if the webhook was successfully created. Requests must be sent as an
application/json-encoded dictionary with the required fields subscription_url and event
样品申请(根据DOCS):
{
"subscription_url": "http://fluidsurveys.com/api/v2/callback/",
"event": "response_complete",
"survey": 1,
"collector": 1
}
我在这里做错了什么?调查和收集器是可选的参数,我没有看到我体内的json存在问题。
答案 0 :(得分:1)
我猜你可能需要使用像Oj
(https://github.com/ohler55/oj)这样的库将请求体转换为JSON。使用Oj
库:
requestBody = {
'subscription_url' => 'http://glacial-spire-test.herokuapp.com/hook/response_created_callback',
'event' => 'response_complete'
}
url = Typhoeus::Request.new("https://fluidsurveys.com/api/v2/webhooks/subscribe/",
userpwd: "username_test:password_test",
method: :post,
body: Oj.dump(requestBody, mode: :compat),
headers: { 'Content-Type' => "application/json"})
response = url.run.body
puts response
关键路线是:
body: Oj.dump(requestBody, mode: :compat)
如果您需要将任何JSON内容加载到Ruby,只需使用Oj.load