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:

app.get

Use this in your console to make an HTTP GET request to a path in your Rails app.

# Useful when needing to make an HTTP GET request of your site within a console, and watch the logs

Rails.logger.log_level = 0 # Set this to 0 to show all logs, just for this session.

# Any querystring parameter can be passed into your request by passing it a
# Hash of key-value pairs.
params = {
  foo: 'bar'
}

# You can pass headers with your request, such as cookies, so you can make a request that
# is for logged-in users. For example, say your website stores the current user's session
# details in a cookie called "_session_id", you can fetch that value by logging in normally
# from your browser, and then inspecting the cookies that were generated, then copy/paste them
# into here to make a request that normally is reserved for authenticated/logged-in users.
headers = {
  cookies: '_session_id=123456abcdefg;'
}

app.get('/users/33', params: params, headers: headers)
#=> you'll see all the output from this request here, such as controller actions, redirections,
#   DB query logs, etc.

app.post

Use this in your console to make an HTTP POST request to a path in your Rails app.

# Similar to app.get, but this makes a POST request to the path you give it
Rails.logger.log_level = 0 # Set this to 0 to show all logs, just for this session.

# This is the body of what you are POSTing to you endpoint.
# It can be a Hash of key/values pairs.
# It can also be a String that is properly encoded as application/x-www-form-urlencoded or multipart/form-data
params = "first_name=Wanda&phone_number=1234567890"

# You can pass headers with your request, such as cookies, so you can make a request that
# is for logged-in users. For example, say your website stores the current user's session
# details in a cookie called "_session_id", you can fetch that value by logging in normally
# from your browser, and then inspecting the cookies that were generated, then copy/paste them
# into here to make a request that normally is reserved for authenticated/logged-in users.
headers = {
  cookies: '_session_id=123456abcdefg;'
}

app.post('/users/33', params: params, headers: headers)
#=> you'll see all the output from this request here, such as controller actions, redirections,
#   DB query logs, etc.

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:

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 an ActionDispatch::IntegrationTest object, then that object’s @response instance variable will point to the same response object.


 Want to get updates in your inbox? Sign up to receive the newsletter!