class ActionDispatch::Response
Experimental RBS support (using type sampling data from the type_fusion project).
      
      # sig/action_dispatch/http/response.rbs class ActionDispatch::Response def committed?: () -> false end
end
end
puts response.body
get(‘/’)
def test_print_root_path_to_console
class DemoControllerTest < ActionDispatch::IntegrationTest
controller response to the console:
For example, the following demo integration test prints the body of the
TestResponse (which inherits from Response) for this purpose.
and Integration::RequestHelpers#post return instances of
more detail. Methods such as Integration::RequestHelpers#get
Integration tests will likely also want to inspect responses in
ActionController::Metal#headers.
few additional methods that delegate to attributes of the Response such as
ActionController::Metal#response. ActionController::Metal also provides a
The Response object for the current request is exposed on controllers as
from integration tests).
back to the web browser) or a TestResponse (i.e. one that is generated
either represent a real HTTP response (i.e. one that is meant to be sent
retrieve the current state of the response, or customize the response. It can
Represents an HTTP response generated by a controller action. Use it to
= Action Dispatch Response
:nodoc:
def self.create(status = 200, headers = {}, body = [], default_headers: self.default_headers)
def self.create(status = 200, headers = {}, body = [], default_headers: self.default_headers) headers = merge_default_headers(headers, default_headers) new status, headers, body end
def self.merge_default_headers(original, default)
def self.merge_default_headers(original, default) default.respond_to?(:merge) ? default.merge(original) : original end
def abort
def abort if stream.respond_to?(:abort) stream.abort elsif stream.respond_to?(:close) # `stream.close` should really be reserved for a close from the # other direction, but we must fall back to it for # compatibility. stream.close end end
def assign_default_content_type_and_charset!
def assign_default_content_type_and_charset! return if media_type ct = parsed_content_type_header set_content_type(ct.mime_type || Mime[:html].to_s, ct.charset || self.class.default_charset) end
def await_commit
def await_commit synchronize do @cv.wait_until { @committed } end end
def await_sent
def await_sent synchronize { @cv.wait_until { @sent } } end
def before_committed
def before_committed return if committed? assign_default_content_type_and_charset! merge_and_normalize_cache_control!(@cache_control) handle_conditional_get! handle_no_content! end
def before_sending
def before_sending # Normally we've already committed by now, but it's possible # (e.g., if the controller action tries to read back its own # response) to get here before that. In that case, we must force # an "early" commit: we're about to freeze the headers, so this is # our last chance. commit! unless committed? @request.commit_cookie_jar! unless committed? end
def body
Returns the content of the response as a string. This contains the contents
def body @stream.body end
def body=(body)
def body=(body) if body.respond_to?(:to_path) @stream = body else synchronize do @stream = build_buffer self, munge_body_object(body) end end end
def body_parts
def body_parts parts = [] @stream.each { |x| parts << x } parts end
def build_buffer(response, body)
def build_buffer(response, body) Buffer.new response, body end
def charset
The charset of the response. HTML wants to know the encoding of the
def charset header_info = parsed_content_type_header header_info.charset || self.class.default_charset end
def charset=(charset)
response.charset = 'utf-16' # => 'utf-16'
it sets the charset to +default_charset+.
Sets the HTTP character set. In case of +nil+ parameter
def charset=(charset) content_type = parsed_content_type_header.mime_type if false == charset set_content_type content_type, nil else set_content_type content_type, charset || self.class.default_charset end end
def close
def close stream.close if stream.respond_to?(:close) end
def code
def code @status.to_s end
def commit!
def commit! synchronize do before_committed @committed = true @cv.broadcast end end
def committed?; synchronize { @committed }; end
Experimental RBS support (using type sampling data from the type_fusion project).
      
      def committed?: () -> false
This signature was generated using 1 sample from 1 application.
def committed?; synchronize { @committed }; end
def content_type
def content_type super.presence end
def content_type=(content_type)
the character set information will also be included in the content type
If a character set has been defined for this response (see charset=) then
response.content_type = "text/plain"
you could write this:
Sets the HTTP response's content MIME type. For example, in the controller
def content_type=(content_type) return unless content_type new_header_info = parse_content_type(content_type.to_s) prev_header_info = parsed_content_type_header charset = new_header_info.charset || prev_header_info.charset charset ||= self.class.default_charset unless prev_header_info.mime_type set_content_type new_header_info.mime_type, charset end
def cookies
Returns the response cookies, converted to a Hash of (name => value) pairs
def cookies cookies = {} if header = get_header(SET_COOKIE) header = header.split("\n") if header.respond_to?(:to_str) header.each do |cookie| if pair = cookie.split(";").first key, value = pair.split("=").map { |v| Rack::Utils.unescape(v) } cookies[key] = value end end end cookies end
def delete_header(key); @headers.delete key; end
def delete_header(key); @headers.delete key; end
def each(&block)
def each(&block) sending! x = @stream.each(&block) sent! x end
def get_header(key); @headers[key]; end
def get_header(key); @headers[key]; end
def handle_no_content!
def handle_no_content! if NO_CONTENT_CODES.include?(@status) @headers.delete CONTENT_TYPE @headers.delete "Content-Length" end end
def has_header?(key); @headers.key? key; end
def has_header?(key); @headers.key? key; end
def initialize(status = 200, headers = nil, body = [])
def initialize(status = 200, headers = nil, body = []) super() @headers = Headers.new headers&.each do |key, value| @headers[key] = value end self.body, self.status = body, status @cv = new_cond @committed = false @sending = false @sent = false prepare_cache_control! yield self if block_given? end
def media_type
def media_type parsed_content_type_header.mime_type end
def message
response.message # => "Not Found"
response.status = 404
response.message # => "OK"
response.status = 200
Returns the corresponding message for the current HTTP status code:
def message Rack::Utils::HTTP_STATUS_CODES[@status] end
def munge_body_object(body)
def munge_body_object(body) body.respond_to?(:each) ? body : [body] end
def parse_content_type(content_type)
def parse_content_type(content_type) if content_type && match = CONTENT_TYPE_PARSER.match(content_type) ContentTypeHeader.new(match[:mime_type], match[:charset]) else NullContentTypeHeader end end
def parsed_content_type_header
Small internal convenience method to get the parsed version of the current
def parsed_content_type_header parse_content_type(get_header(CONTENT_TYPE)) end
def rack_response(status, headers)
def rack_response(status, headers) if NO_CONTENT_CODES.include?(status) [status, headers, []] else [status, headers, RackBody.new(self)] end end
def reset_body!
def reset_body! @stream = build_buffer(self, []) end
def response_code
def response_code @status end
def send_file(path)
def send_file(path) commit! @stream = FileBody.new(path) end
def sending!
def sending! synchronize do before_sending @sending = true @cv.broadcast end end
def sending?; synchronize { @sending }; end
def sending?; synchronize { @sending }; end
def sending_file=(v)
def sending_file=(v) if true == v self.charset = false end end
def sent!
def sent! synchronize do @sent = true @cv.broadcast end end
def sent?; synchronize { @sent }; end
def sent?; synchronize { @sent }; end
def set_content_type(content_type, charset)
def set_content_type(content_type, charset) type = content_type || "" type = "#{type}; charset=#{charset.to_s.downcase}" if charset set_header CONTENT_TYPE, type end
def set_header(key, v); @headers[key] = v; end
def set_header(key, v); @headers[key] = v; end
def status=(status)
def status=(status) @status = Rack::Utils.status_code(status) end
def to_a
and body. Allows explicit splatting:
Turns the Response into a Rack-compatible array of the status, headers,
def to_a commit! rack_response @status, @headers.to_hash end
def write(string)
def write(string) @stream.write string end