Tristan Hulbert
Tristan Hulbert

Reputation: 23

Access params of post request to my rails app

I am using Sendgrid to send emails from my rails app. Sendgrid send HTTP POST requests back to my app when events occur on the emails that I send - such as when an email is opened.

Sendgrid requires a URL to be provided which post requests are sent to. Mine is

my_domain.com/contact_processor

My routes.

resources :contact_processor

I know I can define a specific route, I used resources however and learned that the post request was looking for a create action.

My terminal shows the params being received.

parameters: {"_json"=>[{"ip"=>"66.249.82.220", 
"sg_event_id"=>"YWRREWM4ZmItMzY4YS00MjY1LWE3YTAtOTI0MzcwNTJhMTBj", 
"sg_message_id"=>"YZ8_123AQzOXoILstbNB4Q.filter0018p1las1.11190.577E6B3116.0", 
"useragent"=>"Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko Firefox/11.0 (via 
 ggpht.com GoogleImageProxy)", "event"=>"open", "foo_id"=>"19", 
"email"=>"[email protected]", "timestamp"=>1467905735, 
"bar_id"=>"23"}], "contact_processor"=>{}}

I'm wanting to access the foo_id, bar_id, and event values so as to use them to update attributes of objects within my app.

What appeared to be a fairly simple task has stumped me.

Any help on how to access these and a bit of an explanation as to what I'm dealing with here would be greatly appreciated.

Upvotes: 0

Views: 50

Answers (1)

Brad Werth
Brad Werth

Reputation: 17647

You can access them in the controller action hit by the callback, just like normal.

1.9.3-p551 :024 > params['_json']
 => [{"ip"=>"66.249.82.220", "sg_event_id"=>"YWRREWM4ZmItMzY4YS00MjY1LWE3YTAtOTI0MzcwNTJhMTBj", "sg_message_id"=>"YZ8_123AQzOXoILstbNB4Q.filter0018p1las1.11190.577E6B3116.0", "useragent"=>"Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko Firefox/11.0 (via \n ggpht.com GoogleImageProxy)", "event"=>"open", "foo_id"=>"19", "email"=>"[email protected]", "timestamp"=>1467905735, "bar_id"=>"23"}] 
1.9.3-p551 :025 > params['_json'].first['ip']
 => "66.249.82.220" 

Upvotes: 1

Related Questions