发送 JSON Webhook 响应
Sending JSON Webhook Response
我正在尝试通过 JSON 接收来自网络服务的请求,如果令牌与其他一些识别信息正确,则发送成功的响应消息,否则发送正确的错误消息
post "/hook/foo/bar" do
puts request.env
if request.env['TOKEN'] === "secret_code"
HTTParty.post("https://hook.com/hooks/catch/foo/bar/",
{
:body => @info.to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
})
[200, {}, "Success"]
else
[400, {}, "Authorization Failed"]
end
发送挂钩的服务 (Zapier) 说它发送成功,但我没有向他们响应任何我可以使用的有意义的数据。我认为我的回复格式有误,但我不确定是怎么回事。
这里是来自 Zapier 平台团队的 David。
根据 sinatra docs,您可以 return:
An Array with three elements: [status (Fixnum), headers (Hash), response body (responds to #each)]
所以您没有发回关于内容类型的任何提示,而是一个字符串作为正文。这是有效的,你的钩子成功了,但你可以做得更好!
Zapier 将传出挂钩的响应解析为 JSON,因此最好将其发回。
我刚刚测试了以下示例:
require 'sinatra'
require 'json'
get '/' do
'hello world!'
end
post '/hook' do
{message: 'great!'}.to_json
end
我的回复被解析了!
如果您想设置状态代码,老实说,最简单的方法是在 return 之前随时使用函数 status(400)
。也就是说,401
可能是您想要 Unauthorized", rather than
400` 的代码。不管怎样,zapier 都会将 运行 标记为错误。
如果您还有其他问题,请告诉我!
我正在尝试通过 JSON 接收来自网络服务的请求,如果令牌与其他一些识别信息正确,则发送成功的响应消息,否则发送正确的错误消息
post "/hook/foo/bar" do
puts request.env
if request.env['TOKEN'] === "secret_code"
HTTParty.post("https://hook.com/hooks/catch/foo/bar/",
{
:body => @info.to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
})
[200, {}, "Success"]
else
[400, {}, "Authorization Failed"]
end
发送挂钩的服务 (Zapier) 说它发送成功,但我没有向他们响应任何我可以使用的有意义的数据。我认为我的回复格式有误,但我不确定是怎么回事。
这里是来自 Zapier 平台团队的 David。
根据 sinatra docs,您可以 return:
An Array with three elements: [status (Fixnum), headers (Hash), response body (responds to #each)]
所以您没有发回关于内容类型的任何提示,而是一个字符串作为正文。这是有效的,你的钩子成功了,但你可以做得更好!
Zapier 将传出挂钩的响应解析为 JSON,因此最好将其发回。
我刚刚测试了以下示例:
require 'sinatra'
require 'json'
get '/' do
'hello world!'
end
post '/hook' do
{message: 'great!'}.to_json
end
我的回复被解析了!
如果您想设置状态代码,老实说,最简单的方法是在 return 之前随时使用函数 status(400)
。也就是说,401
可能是您想要 Unauthorized", rather than
400` 的代码。不管怎样,zapier 都会将 运行 标记为错误。
如果您还有其他问题,请告诉我!