From time to time I need to get all the Rails logs that are output when making an HTTP request, and depending on the log level in our production environment, not all of them make it to our log analyzers. Thankfully, Rails has you covered with a few handy commands you can run from a Rails console. You can find them inside of the ActionDispatch::Integration::RequestHelpers
class:
- Rails 4
ActionDispatch::Integration::RequestHelpers
- Rails 5
ActionDispatch::Integration::RequestHelpers
- Rails 6
ActionDispatch::Integration::RequestHelpers
app.get
Use this in your console to make an HTTP GET request to a path in your Rails app.
app.post
Use this in your console to make an HTTP POST request to a path in your Rails app.
app.process
app.get
and app.post
are simply convenience methods that call out to app.process
. As noted in the documentation below, it’s rarely used and you should favor using one of the HTTP method aliases: put
, get
, post
, etc.
See the documentation here:
- Rails 4
ActionDispatch::Integration::Session
- Rails 5
ActionDispatch::Integration::Session
- Rails 5
ActionDispatch::Integration::Session
process(method, path, params: nil, headers: nil, env: nil, xhr: false, as: nil)
Performs the actual request.
method
: The HTTP method (GET, POST, PATCH, PUT, DELETE, HEAD, OPTIONS) as a symbol.
path
: The URI (as a String) on which you want to perform the request.
params
: The HTTP parameters that you want to pass. This may be nil, a Hash, or a String that is appropriately encoded (application/x-www-form-urlencoded or multipart/form-data).
headers
: Additional headers to pass, as a Hash. The headers will be merged into the Rack env hash.
env
: Additional env to pass, as a Hash. The headers will be merged into the Rack env hash.This method is rarely used directly. Use
#get
,#post
, or other standard HTTP methods in integration tests.#process
is only required when using a request method that doesn’t have a method defined in the integrations tests.This method returns a
Response
object, which one can use to inspect the details of the response. Furthermore, if this method was called from anActionDispatch::IntegrationTest
object, then that object’s@response
instance variable will point to the same response object.