Rails, how do you access the raw request data at the absolute lowest level?
Asked Answered
T

2

7

When is the Rails request object available at the earliest time during the request lifecycle? Essentially, when is the request first available as a request object, and in which object? ActionDispatch?

Can you access request parameters from Tester::Application? If so, how? If not, what about using the environment? When is that information set?

Tereasaterebene answered 7/12, 2011 at 21:20 Comment(2)
You need to clean up and clarify your question a bit. Are you asking how to get access to all request params during a controller action? If so, that's as easy as writing puts request in a controller action and looking at the rails console.Gefen
This was a bit vague, but essentially, when is the request first available as a request object, in which object? ActionDispatch? etcTereasaterebene
G
12

The Rack webserver creates the request object and then ActionDispatch inherits from it. So essentially, you'd be able to access the Rack::Request or ActionDispatch::Request objects within the middleware of the app.

Rack::Request

https://github.com/rack/rack/blob/master/lib/rack/request.rb

ActionDispatch::Request

https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/http/request.rb

Gefen answered 8/12, 2011 at 16:13 Comment(0)
P
3

A middleware is probably the best way to access this data. I made a simple gem for setting up a raw HTTP request/response log in Rails using the classes mentioned in the accepted answer.

https://github.com/andrhamm/marcopolo

Putty answered 6/5, 2014 at 13:35 Comment(2)
Thanks for making this. It's exactly what I needed to scrape out tokens from an api service so that I could hit it with curl.Tedman
@JacobDalton no problem, please let me know how it works out for you or open an issue on the github page for ideas for enhancementsPutty

© 2022 - 2024 — McMap. All rights reserved.