Reputation: 544
I have to send a post request in httparty and get response my json params are
{
"webhook": {
"topic": "shop/update",
"address": "http://2350d6c0.ngrok.io/shopify_stores/update_shop_info",
"format": "json"
}
}
And i am using httparty params as
begin
response = HTTParty.post("#{url}",
{
:body => [
{
"webhook" => {
"topic" => "shop\/update",
"address" => "http:\/\/15ec3a12.ngrok.io\/shopify_stores\/update_shop_info", #place your url here
"format" => "json"
}
}
].to_json,
:headers => { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
})
byebug
rescue HTTParty::Error
end
but it repsonse is
Required parameter missing or invalid
Upvotes: 0
Views: 625
Reputation: 102443
The smoothest way to work with HTTParty is by creating clients instead of using it in a procedural fashion.
class MyApiClient
include HTTParty
base_uri 'example.com'
format :json
# this is just an example of how it is commonly done
def initalize(api_key = nil)
@api_key = api_key
end
def post_something(data)
self.class.post("/some_path", data)
end
end
This will let you do:
client = MyApiClient.new()
puts client.post_something({ foo: "bar" })
You don't need handle setting headers or encoding the body - HTTParty will handle that for you. Thats kind of the whole point of the library - if you want to grunt it out procedurally just use Net::HTTP which is part of the stdlib.
Upvotes: 1