Improperly accessing parameters - ruby-on-rails

The following action is receiving parameters
Started POST "/transactions/214/success?locale=it" for 5.171.1.159 at 2018-10-05 13:30:54 +0200
Processing by TransactionsController#success as HTML
Parameters: {"response-signature-base64"=>"JgO1UzlOubzB0Ic4qSaphxJm4hrFbeR/vF6B/ZanP1s=", "response-signature-algorithm"=>"HmacSHA256", "locale"=>"en", "id"=>"214"}
However, the cation is not properly processing the parameters. As a way of proof, the controller action has:
def success
puts params
puts "reponse signature base 64"
puts params[:'response-signature-base64']
but the log is not writing any of the above parametric data. What should I be doing to properly access these parameters?

Compare
Started POST "/transactions/214/success?locale=it"
Processing by TransactionsController#success as HTML
with code
def payment_success
You have a route mismatch.
EDIT
puts writes to stdout, not to rails log.

Related

Capture response sent to webhook rails?

I'm working with my first webhook in my rails application. My HookController is receiving the following response:
Started POST "/hooks/callback" for 127.0.0.1 at 2014-11-18 14:10:13 -0500
Processing by HooksController#callback as HTML
Parameters: {"Status"=>"<?xml version=\"1.0\"?>\n<BackgroundReports userId=\"username\" ...
Completed 401 Unauthorized in 1ms
I'm trying parse the XML that is returned in the parameters and I'm getting stuck.
I have tried starting with the following in my controller raising :
webHook = JSON.parse(params[:status])
raise webHook.inspect
But it appears as if its not capturing the params.
How do I capture the params and parse the XML in my controller?
I was able to troubleshoot the problem myself.
I am using Devise for my User model. That was driving the 401 Unauthorized error. As a result I was not able to to pull and parse the params.
I found the answer in a similar question -> Webhook returning 401 in Rails on Stakoverflow. Adding the following allowed Devise to authenticate the response. I could parse the params from there:
skip_before_filter :verify_authenticity_token, :authenticate_user!

Access params inside params?

Form on submit sends params[:model], which inside have other hashes, like :attribute_1=>value_1
For example, server log:
Started POST "/tests/1/create_answer" for 127.0.0.1 at 2013-05-26 04:36:09 +0400
Processing by TestsController#create_answer as JS
Parameters: {"utf8"=>"✓", "authenticity_token"=>"sFZIbaxVSnf1auEbsGMdVVl7dg0OWXKnFA0pl1aXA9o=", "answer"=>{"user_answer"=>"ssss", "position"=>"96", "key_id"=>"2"}, "commit"=>"", "id"=>"1"}
Is there way to acces any of this attributes in controller, some kind of:
#key=Key.find(params[:answer][:key_id])
Tried this, returns nil.

"Params" not working to handle JSON parameters in Rails

I have a webservice application, which in one of my controllers there's an action that receives a POST from an external application.
Here's the log from my heroku app, which confirms that it's coming a Json data parameter:
2012-01-05T18:26:07+00:00 app[web.1]: Started POST "/let" for 192.168.1.145 at 2012-01-05 10:26:07 -0800
2012-01-05T18:26:07+00:00 app[web.1]: Processing by LetController#create as HTML
2012-01-05T18:26:07+00:00 app[web.1]: Parameters: {"Id"=>"dfe510c1-56e2-48ae-af0d-c4b265798f2e", "FormId"=>"5025", "Answers"=>[{"IdQuestion"=>"cfe0"
, "Value"=>"One", "FilledDate"=>"/Date(1325795160323-0200)/"}], "MSISDN"=>"8984223722"}
So, how can I access this json parameter on my method? I've tried lots of things like params[:formid], params["formid"], ActiveSupport::JSON.decode(request.body) etc etc.. and when pass to view, nothing is rendered, it's like it doesn't get anything.
It looks as though you should be able to access your params as follows:
params[:Id]
params[:FormId]
params[:Answers]
params[:Value]
params[:FilledDate]
params[:MSISDN]

How do I get rails to parse params from CURL as JSON or XML instead of a string?

How do I get rails to parse params as JSON or XML instead of a string? I'm using rails 3.0.7.
Here's what I want to happen.
Parameters: {"user"=>{"email"=>"user#blah.com", "password"=>"[FILTERED]"}}
Here's what happens
# controller
def create
logger.debug params
end
# curl from command line
curl -i -H 'Content-Type:application/xml' -H 'Accept:application/xml' -X POST -d "<user><email>user#blah.com</email><password>password</password></user>" http://localhost:3000/api/v1/sessions.xml
# response
Started POST "/api/v1/sessions" for 127.0.0.1 at 2011-05-14 02:16:23 -0700
Processing by Api::V1::SessionsController#create as XML
Parameters: {"<user><email>user#blah.com</email><password>password</password></user>"}
{"<user><email>user#blah.com</email><password>password</password></user>", "action"=>"create", "controller"=>"api/v1/sessions", "format"=>"xml"}
Same thing happens with JSON.
An HTTP POST is typically name/value pairs.
Rails is doing its best try and figure out what you've passed it and turn it into a params hash, but it doesn't really make sense.
You can access the request directly:
def create
string = request.body.read
# if string is xml
object = Hash.from_xml(string)
# elsif string is JSON
object = ActiveSupport::JSON.decode(string)
end
Run rake middleware, check that the ActionDispatch::ParamsParser is used.
Take a look at actionpack-3.0.7/lib/action_dispatch/middleware/params_parser.rb
This is the middleware that's parsing the providing data to params hash. My guess is that the problem is with curl, as it doesn't post the request as you think it should. Maybe it needs a space after a colon (in headers), I don't know. I found this on google: http://snippets.dzone.com/posts/show/181.

rails 3, am I not using redirect_to correctly?

This should be the easiest thing in the world, but a POST redirect is not working.
A remote system normally POSTs data to one of my methods "mainhandler'
In one situation, though, it POSTs data to a different handler "noaudio" which is SUPPOSED to simply erase a session variable then "pass the POST" on to the normal handler ("mainhandler")
My route:
match '/mainhandler' =>"widgets#mainhandler"
My code:
def noaudio
puts "**** NOAUDIO"
session[:audiofile] = ""
redirect_to "/mainhandler"
end
def mainhandler
puts "**** NEVER GETS HERE FROM NOAUDIO ??"
end
what I see in the log (below) it LOOKS like the redirect happens but the handler "mainhandler" is never executed... I see the "puts" in the noaudio handler and I see what looks like a good redirect... but mainhandler never gets executed.
**** NOAUDIO
Started POST "/noaudio" for 1.2.3.4 at 2011-03-07 09:39:22 -0800
Processing by WidgetsController#noaudio as HTML
Parameters: blah blah blah
Redirected to http://mydomain.com/mainhandler
Completed 302 Found in 1ms
Am I using redirect_to incorrectly? or does it not work for a POST? (Although I would presumably still see at least a GET)
Redirects do not work for POSTs indeed.
You may want to take a look at that for some more info :
redirect_to using POST in rails
If you really must have this middleman and want to POST on the data to a new URL, you could use Ruby's Net::HTTP class and pass on the data.
I think you should think twice though and ask yourself if this is really what you want to do.

Resources