csakon
csakon

Reputation: 611

Can't POST results of ActiveRecord query via HTTParty

I'm running a simple Sinatra app where I need to query some data and send a POST via JSON to a webhook URL for processing. I'm not sure how to properly format the records I've retrieved for HTTParty.

The contacts are printing to /account/{account_id}/contacts.json how I want them, they data just isn't being sent successfully.

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

Upvotes: 0

Views: 106

Answers (1)

Mayank
Mayank

Reputation: 727

It seems the error is

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

It does not found any body in your request. You are sending the parameters body and head inside a hash.

Try this:

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

You can checkout this post also for any more information regarding HTTP post Here

Hope this helps..

Upvotes: 1

Related Questions