Rails impossible to get post response in rack middleware - ruby-on-rails

I made a rails 3 rack middleware to log users actions with request = Rack::Request.new(env).
So i send to my database the request.fullpath and request.user_agent, as detailed below:
My issue appears I want to get the POST response too (to get ids, people name extracted from the JSON payload ...).
So i get the response = Rack::Response.new(request.path). But when i print response.body, i have only my request.path, and the request.params does not contain anything ...
By looking at the response with Firebug, I can see all the data I want.
Thanks for your responses.

Problem resolved !
I finally add status, headers, body = #app.call(env) to my middleware and send the body variable to my service. For each POST request, body contains all the post response I want.

Related

Ruby on Rails how to set new params on HTTParty before redirect?

I have a POST request with HTTParty, this post validates my params and then redirect me to another page. So there are two requests.
But the second request also require params, how can I pass this params to HTTParty before the redirect?
First request:
response = HTTParty.post(URL_TO_SITE, headers: headers, body: params)
# What I need it's something like this
response.before_redirect(params: new_params)
I don't know if this is the correct way, so how can I do that?
Disable follow redirect (as described here: https://github.com/jnunemaker/httparty/issues/380)
Load initial request
Save cookies
Do manual redirect with the cookies

Sending form params as body for Guzzle POST request

I'm a bit confused about Guzzle's deprecation of sending body parameters, and throwing an exception if $body is an array. The recommended way is seemingly to replace body parameters with form_params.
I'm posting to the eventbrite API here
https://www.eventbrite.com.au/developer/v3/quickstart/
and when doing the following:
$response = $this->guzzleClient->request('POST', $item->eventbriteEndpoint, ['form_params' => $item->parameters]);
It doesn't work (and actually responds like it's received a GET request)
After much trial and error, I discovered I need to json_encode the parameters into a json string and add them into the body like so
$response = $this->guzzleClient->request('POST', $item->eventbriteEndpoint, ['body' => json_encode($item->parameters));
So what is going on here, is form_params the correct replacement for body, and why? Is json_encoding body the correct way to do it, and if so why is form_params mentioned everywhere and this not common knowledge? Something is wrong here, is it
My understanding of form_params and body parameters in a guzzle POST request (most likely)
Guzzle doesn't handle POST requests correctly (unlikely)
The Eventbrite API is not receiving POST requests correctly.
I find guzzle to be incredibly opaque tbh, xdebugging down the rabbit hole usually asks more questions than it answers.
I've actually just discovered something even more bizarre on the Eventbrite end - a POST request to create a new event uses form_params, and updating an event requires the params as json in the body. What.

How to get POST request from API with rails

I got POST request from API. I checked content-type, it's x-www-form-urlencoded, but body request also contains JSON. How can I parse from JSON in hash?
Parameters of request in console: Screenshot || Parameters: {"vpbx_api_key"=>"etxojfklr6nue6tl627pn5sdi0koov7t", "sign"=>"ad0c49034c8d83a7d7f1b433afc2ed5a9aa08d933dba3724062aed0c3d1a79bb", "json"=>"{\"entry_id\":\"MjYyNjQ2NzM1Njo0Mg==\",\"call_id\":\"MToxMDAxNDAzOTo0Mjo4Mjc2NzEzMzk=\",\"timestamp\":1485939839,\"seq\":2,\"call_state\":\"Disconnected\",\"location\":\"abonent\",\"from\":{\"number\":\"79268220697\",\"taken_from_call_id\":\"MToxMDAxNDAzOTo0Mjo4Mjc2NzEwOTA6MQ==\"},\"to\":{\"extension\":\"2\",\"number\":\"79154612023\",\"line_number\":\"74953749768\"},\"disconnect_reason\":1100}"}
If you want to keep the form encoded that way, you will have the parameters in the params collection. That means you could be parsing that JSON doing something as:
def my_method_to_process_the_post
parsed = JSON.parse params[:json]
end
'json' is the key for the json that you are receiving in the payload.
For historical evolution of JSON parsing you may want to check this thread.

Receiving a request with no accessible data. ActionDispatcher, therefore, extracts no usable params

I'm getting a CORS POST request via OPTIONS sent to my app. It has no content-type set for the request.
It successfully gets to the right Controller action, but there is no accessible data. If I type params, there is nothing I can touch.
I did, however, discover that if I created Rack Middleware, and read the env['rack.input'], I could find all the data in the request I was looking for. So I wrote this :
env['CONTENT_TYPE'] = 'application/js'
rack_input = env['rack.input'].read
params = CGI::parse(rack_input).to_json
env['rack.input'] = StringIO.new params
env['rack.input'].rewind
status, headers, response = #app.call env
And magically, now in my controller, I can type params and see that ActionDispatcher successfully extracted the key/values from the request and make them accessible in my controller.
There's something suspicious about this. Is there are more appropriate way to extract OPTIONS requests and their respective data?
The OPTIONS call should not deal with data at all. It's a preflighted request to determine which actions are allowed using when using CORS.
RFC:
http://greenbytes.de/tech/webdav/rfc2616.html#rfc.section.14.7
The call will return with the allowed CORS HTTP verbs and a POST request should follow right after if POSTs are allowed on the server.

Sending POST requests from Rails controller for authentication

I am quite new to Ruby and RoR, and have been struggling with a problem and am not getting anywhere.
Basically I am building a "proxy" webservice that will handle certain requests, passing them to a third party website. The response received from the third party website is in HTML, which will then be parsed and an appropriate XML response will be given.
I'm at a point where I need to send POST requests via my controller in Rails to authenticate a user, which I'm doing using this code:
require "net/http"
require "uri"
uri = URI.parse("http://myurl.com/members.cgi")
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Post.new(uri.request_uri)
request.set_form_data({"email_login" => "user#email.com", "password_login" => "password"})
response = http.request(request)
My problem lies on the response I am receiving. This specific POST request, when successful (i.e. user has successfully authenticated), is returning a 302 redirect. That in itself is not an issue, as I can follow the redirect by getting the 'location' header value:
redirectLocation = response['location']
Where I'm getting stuck is in keeping myself authenticated when following the redirect with the following line:
redirectResponse = Net::HTTP.get_response(URI.parse(redirectLocation))
This follows the redirect but return a response showing I am not authenticated??
I don't really understanding why this is happening. I can see there is an authentication cookie being returned with my original response by reading:
response['cookie']
So finally my question is what do I need to do to get the server to recognise my authentication status? Pass the cookie with my second request somehow? If so, how do I do it?
Many thanks in advance for your time!
Rog
Yes you need to set the cookie. I think it probably give you some kind of a session id. Which you need to pass with every request.
look at this code snippet for a example on how to pass on a cookie that you get as a response with your new requests.

Resources