无法通过 HTTParty POST ActiveRecord 查询结果

Can't POST results of ActiveRecord query via HTTParty

我是 运行 一个简单的 Sinatra 应用程序,我需要查询一些数据并通过 JSON 将 POST 发送到 webhook URL 进行处理。我不确定如何正确格式化我为 HTTParty 检索到的记录。

联系人正在按照我的要求打印到/account/{account_id}/contacts.json,只是他们的数据没有成功发送。

app.rb

get "/account/:account_id/contacts.json" do
  @contacts = Contact.where("sfid = ?", params[:account_id])
  HTTParty.post("https://hooks.webapp.com/hooks/catch/387409/1us4j3/",
  { 
    :body => [ @contacts ].to_json,
    :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
  })
  @contacts.to_json
end

看来错误是

2.3.1/lib/ruby/2.3.0/net/http/generic_request.rb:183:in `send_request_with_body'

在您的请求中没有找到任何正文。您正在散列中发送参数 body 和 head 。

试试这个:

get "/account/:account_id/contacts.json" do
  @contacts = Contact.where("sfid = ?", params[:account_id])
  HTTParty.post("https://hooks.webapp.com/hooks/catch/387409/1us4j3/",
    :body =>  @contacts.to_json,
    :headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
  )
  @contacts.to_json
end

您还可以查看此 post 以获取有关 HTTP post Here

的更多信息

希望这对您有所帮助..