2017-01-13 3 views
0

Ich habe zu senden Post-Anforderung in httparty und bekommen Antwort meine Json params sindWie json params in Httparty

{ 
    "webhook": { 
    "topic": "shop/update", 
    "address": "http://2350d6c0.ngrok.io/shopify_stores/update_shop_info", 
    "format": "json" 
    } 
} 

Und ich bin mit httparty params als

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 

aber es bauen repsonse ist

Erforderliche Parameter fehlen oder sind ungültig

+0

Ok. Gibt es einen Teil des Controllers, in dem 'def something_params' steht, unter' private'? Wenn ja, kannst du das in deine Frage stellen? –

+0

Ich benutze sie nicht nur senden Sie eine Post-Anfrage an den Server und Speichern der Antwort –

Antwort

1

die glatteste wa Um mit HTTParty zu arbeiten, müssen Sie Clients erstellen, anstatt sie prozedural zu verwenden.

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 

So können Sie tun:

client = MyApiClient.new() 
puts client.post_something({ foo: "bar" }) 

Sie brauchen nicht zu behandeln Header Einstellung oder den Körper kodiert - HTTParty wird, dass für Sie. Das ist der ganze Sinn der Bibliothek - wenn Sie es prozedural abgrunzen wollen, benutzen Sie einfach Net::HTTP, was Teil der stdlib ist.