# Working with HTTP Responses in Rails

*Published on March 10, 2025*

Tags: Controllers

---


_This is the fourth post in my series on_ [_Rails Controllers_](/tag/controllers)_._

HTTP is a request response protocol. The browser (client) sends an HTTP request to the server (Rails application), which parses that request and generates the HTTP response, typically an HTML page, and sends it to the browser. The browser then renders that HTML on the screen.

In the previous post on&nbsp;[requests in Rails](/http-requests-in-ruby-on-rails), we learned how you can work with the request object to access meaningful information on the incoming HTTP request. In this post, we'll take a look at how you can work with the outgoing response. While it’s less common to handle responses directly—since Rails manages most of that for you—there are situations where working with response objects is necessary.

## Accessing the Response Object

In a Rails controller, the&nbsp;`response`&nbsp;object represents&nbsp;**the HTTP response that your application is preparing to send back to the client**&nbsp;(browser, API client, etc.). You can access the response in your controllers using the&nbsp;`response`&nbsp;object, which is an instance of the&nbsp;`ActionDispatch::Response`&nbsp;class.

```ruby
class ProjectsController < ApplicationController
  def show
    klass = response.class  # => ActionDispatch::Response
    code = response.code    # => 200
    message = response.status_message # => "OK"

    # Get and set headers
    content_type_options = response.headers["x-content-type-options"]
    response.headers["Content-Type"] = "application/json"
  end
end
```

Like the&nbsp;`request`&nbsp;attribute, the&nbsp;`response`&nbsp;attribute comes from the&nbsp;`ActionController::Metal`&nbsp;class. A bunch of common and useful methods are delegated to the&nbsp;`response`&nbsp;object.

```ruby
module ActionController
  class Metal
    attr_internal_reader :response

    delegate :headers, :status, :location, :content_type, :media_type, to: "@_response" 
    delegate :status=, :location=, :content_type=, to: "@_response"
  end
end
```

Whenever you call any of the above methods, like reading the status, accessing the&nbsp;`content_type`, or setting the&nbsp;`location`—Rails forwards those calls to the underlying&nbsp;`response`&nbsp;object.

💡 Remember that&nbsp;`attr_internal_reader`&nbsp;declares an attribute reader&nbsp;`response`&nbsp;backed by an internally-named instance variable, in our case&nbsp;`@_response`.

Now let's inspect some of the commonly used methods on the&nbsp;`ActionDispatch::Response`&nbsp;object.

## What can you do with a Response?

In Rails controllers, the&nbsp;`response`&nbsp;object provides several useful methods for inspecting and manipulating the outgoing HTTP response.

In practice,&nbsp;**you don't work directly with**&nbsp;**`response`**&nbsp;**that often. Instead, you use the various helper methods provided by Rails to manipulate the outgoing response**. That said, it's good to be aware of the response API, and here're some of the commonly used methods.

- **`body`**&nbsp;returns the response body as a string — this is the content that will be sent back to the client. Typically, this is the result of any&nbsp;`render`&nbsp;call made in the action.

```ruby
render plain: "Hello, World!"
puts response.body # => "Hello, World!"
```

- **`body=`**&nbsp;sets or overrides the response body manually. This is useful if you want to bypass Rails’ default rendering process and take full control of the response content.

```ruby
response.body = "Custom response content"
```

- **`charset`**&nbsp;returns the character set (encoding) of the response. HTML wants to know the encoding of the content you’re giving them, so we need to send that along. This is often&nbsp;`UTF-8`&nbsp;for HTML or JSON responses.

```ruby
puts response.charset # => "utf-8"
```

- **`content_type`**&nbsp;returns the content type of the response (MIME type). This is set automatically by&nbsp;`render`&nbsp;and other response-generating methods, but you can also inspect or modify it directly.

```ruby
render json: { message: "Hello" }
puts response.content_type # => "application/json"
```

- **`content_type=`**&nbsp;sets the HTTP response’s content MIME type.

```ruby
response.content_type = "application/xml"
```

- **`cookies`**&nbsp;returns a Hash of cookies that will be sent with the response, in the form of&nbsp;`name => value`&nbsp;pairs. This can be useful if you need to inspect cookies set during the request lifecycle. We'll explore cookies in detail in a future post.

```ruby
cookies[:user_id] = 42
puts response.cookies # => { "user_id" => "42" }
```

- **`location`**&nbsp;returns the location of the response, useful when redirecting the response to another location.

```ruby
redirect_to posts_path
puts response.location # => "http://localhost:3000/posts"
```

- **`message`**&nbsp;returns the standard description for the current HTTP status code. This can be useful for logging or debugging purposes.

```ruby
response.status = 404
puts response.message # => "Not Found"
```

- **`send_file`**&nbsp;sends a file from the server as the response body. This method is often used when serving downloads like PDFs or images directly.

```ruby
send_file Rails.root.join("public", "example.pdf")
```

- **`status=`**&nbsp;Sets the HTTP status code.

```ruby
response.status = 403
```

- **`to_a`**&nbsp;Converts the response into a Rack-compatible array with three elements:&nbsp;`[status, headers, body]`. This is useful if you need to work directly at the Rack level (for example, in middleware or low-level response manipulation).

```ruby
p response.to_a # => [200, { "Content-Type" => "text/html; charset=utf-8" }, ["Rendered page content"]]
```

Even though the response object exposes all these methods for you to manipulate the outgoing response, you should use them directly only if you have a specific, advanced need to bypass or enhance Rails' built-in helpers.

### When to Use Above Methods Directly vs. When to Rely on Rails Helpers

In most Rails controllers, you&nbsp;**should prefer higher-level helpers like**&nbsp;**`render`****,**&nbsp;**`redirect_to`****,**&nbsp;**`head`****, and**&nbsp;**`send_file`**&nbsp;for setting responses. These methods are designed to work with full response lifecycle in Rails, including content negotiation, layout rendering, error handling, and much more.

```ruby
# Set body and content type in one step
render json: { message: "Success" }

# Set status and no content
head :no_content

# Redirect to another page - sets the Location header
redirect_to posts_path
```

**Direct use of the**&nbsp;**`response`**&nbsp;**object is appropriate when:**

- You are writing low-level middleware-like logic inside a controller (rare).
- You need to explicitly inspect or override specific response headers (such as adding a custom header for CORS or security purposes).
- You want to debug or log raw response data during development or in a custom logging concern.
- You are building a very customized response flow that does not fit standard rendering or redirection patterns.

In most standard Rails apps,&nbsp;**you rarely work directly with**&nbsp;**`response`**&nbsp;**inside controllers**. The&nbsp;`response`&nbsp;object is more frequently&nbsp;**used in tests**&nbsp;where you need to inspect the result of a controller action.

Hope that helps clarify things a little.

