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?

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
scottkf
  • 185
  • 3
  • 9
  • 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. – iwasrobbed Dec 08 '11 at 03:26
  • This was a bit vague, but essentially, when is the request first available as a request object, in which object? ActionDispatch? etc – scottkf Dec 08 '11 at 12:43

2 Answers2

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

iwasrobbed
  • 46,496
  • 21
  • 150
  • 195
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

andrhamm
  • 3,924
  • 4
  • 33
  • 46
  • 1
    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. – Jacob Dalton Aug 26 '14 at 19:36
  • @JacobDalton no problem, please let me know how it works out for you or open an issue on the github page for ideas for enhancements – andrhamm Aug 27 '14 at 00:43