Access params of post request to my rails app - ruby-on-rails

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"=>"test#mydomain.com", "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.

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"=>"test#mydomain.com", "timestamp"=>1467905735, "bar_id"=>"23"}]
1.9.3-p551 :025 > params['_json'].first['ip']
=> "66.249.82.220"

Related

current_user is nil from react side , Rails application

I have a Rails server which runs on port 3000, and a react app which runs on port 3001. The problem is:
when I want to create a new object from react side current_user is empty. I can create object from Rails side
without any problem. I have checked the headers of request for both side, "HTTP_COOKIE" are set for both side.
There is no error in console as well. I also put skip_before_action :verify_authenticity_token in application_controller. The output of pp request.headers.env.select{|k, _| k =~ /^HTTP_/} when I am trying to create object from react side, is :
{"HTTP_VERSION"=>"HTTP/1.1",
"HTTP_X_FORWARDED_HOST"=>"localhost:3001",
"HTTP_X_FORWARDED_PROTO"=>"http",
"HTTP_X_FORWARDED_PORT"=>"3001",
"HTTP_X_FORWARDED_FOR"=>"127.0.0.1",
"HTTP_COOKIE"=>
"_ALL THE COOKIES ARE SET HERE ",
"HTTP_CONNECTION"=>"close",
"HTTP_ORIGIN"=>"http://localhost:3000",
"HTTP_X_REQUESTED_WITH"=>"XMLHttpRequest",
"HTTP_X_CSRF_TOKEN"=>"undefined",
"HTTP_ACCEPT_ENCODING"=>"gzip, deflate",
"HTTP_ACCEPT_LANGUAGE"=>"en-US,en;q=0.5",
"HTTP_ACCEPT"=>"*/*",
"HTTP_USER_AGENT"=>
"Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:75.0) Gecko/20100101 Firefox/75.0",
"HTTP_HOST"=>"localhost:3000"}
Any idea what I am missing here?
Your rails session is different than the react session. You will need to implement Authorization header and fill your current_user on before_action.
Check my response here: How to use postman to request an API that is protected with devise?

Getting a 500 Response error after Replaying a recorded browser session using Radview's Webload tool, from HTTP POST attempt

I've been using Radview's Webload IDE tool for a couple of test simulation projects and it has worked well. But for this one scenario where I have a client web session for a login a screen, it would always fail with a 500 Response error for a particular HTTP post as the page loads.
When I try the scenario to load the page manually with a browser it works fine with no issues.
During the recording I would set clear browser cache and cookies and no luck. And I've also tried out many configuration combinations from the "Recording and Script Generatinon Options: Post Data" settings.
/***** WLIDE - URL : http://192.168.2.2/ - ID:2 *****/
wlGlobals.GetFrames = false
wlGlobals.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko"
wlHttp.Get("http://192.168.2.2/")
// END WLIDE
/***** WLIDE - URL : http://192.168.2.2/Api.ashx?c=Images&action=GetSettings - ID:3 *****/
wlHttp.Header["Referer"] = "http://192.168.2.2/"
wlHttp.FormdataEncodingType = 1
wlHttp.ContentType = "application/x-www-form-urlencoded"
wlHttp.FormData["c"] = "Images"
wlHttp.FormData["action"] = "GetSettings"
wlHttp.Post("http://192.168.2.2/Api.ashx"+"?c=Images&action=GetSettings")
// END WLIDE
Anybody with experience with Radview's Webload can give me some suggestions?
I noticed that commenting out the formdata "c" and "actions" lines works. but later I notice a similar error which requires a sessionID in the URL so I'm not sure if I can comment out the formdata "sessionID" line.
To run the API from Webload you need to specify the authorization if its secured.
Using wlHttp.FormData is not the same as adding a parameter to the URL for a POST request.
FormData will be send as part of the post-data request body, while adding it to the URL will send it as a query string - your sever probably expects one form but not the other.
Contact RadView support if you can't get it to work and they'll help you

Unknown GET Request after POST Request

I have an a.shtml page and a form on it. When i submit the form with POST i call a.cgi and redirect the page b.shtml from the cgi with META. But i saw on access.log that a.cgi executes two times. It causes some problems. Why it is called twice and the second one is with GET and how can i avoid this? It is only occurs on Chrome. IE,Firefox is OK
my form:
<form method="post" action="cgi-bin/a.cgi"> ....</form>
meta inside the cgi:
printf("<META HTTP-EQUIV=\"Refresh\" CONTENT=\"3;url='/b.shtml'\ "/>");
access log:
..POST /cgi-bin/a.cgi HTTP/1.1|Host: xxx.xxx.xxx.xxx|Connection: keep-alive|Content-Length: 42|Cache-Control: max-age=0|Origin: .....
..GET /cgi-bin/a.cgi HTTP/1.1|Host: xxx.xxx.xxx.xxx|Connection: keep-alive|User-Agent: Mozilla/5.0 (Windows NT
It sounds like you want to trigger a resubmission of an HTTP POST using a html meta refresh element.
I don't know if this is possible or reliable. Hopefully these terms help you with googling at least.
You may wish to look into using sessions instead.
Edit2:
I found this SO question:
"POST-requesting a location sending Refresh header makes Firefox create GET request but still hold POST data"

Extract Information From Global.ascx File

Is there a way to get the following data from the Application_Error event in the Global.ascx file?
action error came from,
ipaddress error came from,
browser error came from,
browser version error came from,
hostName error came from
??
All that information is contained in the Context.Request property.
Context.Request.Url; // /controller/action?foo=bar so up to you to extract the action
Context.Request.UserHostAddress; // 123.456.789.0123
Context.Request.UserAgent // Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13
And once you are sick of parsing all this crap manually and repeating this code all over again among all your applications you might consider using ELMAH.
hostName error came from
Not sure what you mean here. Isn't that the IP of the client?

Problem sending post request from Flex app to Ruby on Rails back end

I am having trouble getting my flex app to send a POST request to my Rails app. It seems to always send GET.
This is my service declaration:
<mx:HTTPService id="add_email_service" showBusyCursor="true" result="parseJoinResult();" fault="onJoinFault(event)" useProxy="false" />
In my application init function, I set the method to POST:
add_email_service.url = join_url;
add_email_service.method = "POST";
However, my Rails app still sees the request as a post request when I send it. I know this because if I require the request to be POST in my routes.rb file:
# RESTful API for joining a mailing list
map.connect 'mailing_lists/join/:id',
:controller => 'mailing_lists',
:action => 'join',
:conditions => { :method => :post}
the request faults and I can see in my dev log :
"Processing ApplicationController#index (for 127.0.0.1 at 2009-04-23 14:25:35) [GET],
ActionController::MethodNotAllowed (Only post requests are allowed.):"
Does anyone know why this is happening?
Older versions of the Flash Player (in some browsers) would automatically switch your request from POST to GET if there were no variables being sent.
Try adding a param to your request and see if that fixes it.
Integrating Flash Player with Restful Rails?
Get ready for some other issues too:
The Flash Player can only accept HTTP status code of 200, everything else throws an exception and prevents you from getting at the message body.
You can't send PUTS or DELETE HTTP methods
I believe it's literally 200 only.

Resources