Rack::Request - how do I get all headers?
The HTTP headers are available in the Rack environment passed to your app:
HTTP_
Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request.
So the HTTP headers are prefixed with "HTTP_" and added to the hash.
Here's a little program that extracts and displays them:
require 'rack'
app = Proc.new do |env|
headers = env.select {|k,v| k.start_with? 'HTTP_'}
.collect {|key, val| [key.sub(/^HTTP_/, ''), val]}
.collect {|key, val| "#{key}: #{val}<br>"}
.sort
[200, {'Content-Type' => 'text/html'}, headers]
end
Rack::Server.start :app => app, :Port => 8080
When I run this, in addition to the HTTP headers as shown by Chrome or Firefox, there is a "VERSION: HTPP/1.1" (i.e. an entry with key "HTTP_VERSION" and value "HTTP/1.1" is being added to the env hash).
How can I show the headers received by Rack, and how Rails sees them?
You'll be able to use the request.headers
:
#controller action
request.headers.each do |header|
header
end
How can I get request headers in their original format from Rack?
You can get the raw headers in webrick/httpserver.rb from the raw_header instance variable of WEBrick::HTTPRequest:
p req.instance_variable_get("@raw_header")
si.service(req, res)
You can also get it from inside the service method in handler/webrick.rb.
Accessing headers from Sinatra
Try use before
block with headers
method:
before do
headers "HTTP_AUTH" => "test"
headers "Content-Type" => "text/html; charset=utf-8"
end
or in request:
get '/' do
headers['HTTP_AUTH'] = "test"
headers['Cache-Control'] = 'public, max-age=600'
puts headers # show headers on this request
end
Use headers
with is just hash
How to test headers with rspec and rack-test in Sinatra
You should be able to inspect the last_request like so:
last_request.env["HTTP_X_MY_AWESOME_HEADER"]
using RSpec & your example above you would test with:
last_request.env["HTTP_X_MY_AWESOME_HEADER"].should == "It's value"
And hopefully you'll get a green light :)
More info here:
http://www.sinatrarb.com/testing.html#asserting_expectations_about_the_response
HTH
custom request headers in rspec controller test being passed as rack.session
I've been digging through the issue this morning as well. The problem comes from here http://apidock.com/rails/ActionController/TestProcess/process as the method signature looks like this (action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
. This was quite unexpected to me and I'll keep looking though I'm not quite sure why it happens like this.
To get it working you could do
before do
request.headers['X-API-KEY'] = 'somekey'
end
This works, although not exactly what I wanted/expected from the get
method.
Related Topics
How to Write Rake Task to Import Data to Rails App
How to Disable Db:Schema:Dump for Migrations
How to Find Each Instance of a Class in Ruby
Has Anyone Successfully Deployed a Rails Project with Ruby 1.9.1
Optional Arguments with Default Value in Ruby
Ruby Ssl Error - Sslv3 Alert Unexpected Message
How to Make 'Whenever' Gem Work on Windows
Replace Words in a String - Ruby
How to Format This International Phone Number in Rails
Elasticsearch & Tire: Using Mapping and To_Indexed_JSON
How to Read a File from Bottom to Top in Ruby
Using Rbenv Doesn't Work with Sudo
Ruby-Debug19 on Ruby-1.9.3-Preview1
Rails 3.1 - Has_And_Belongs_To_Many Deprecated
Rspec --Init Not Working/ 'Mkd Ir': Invalid Argument - ./C: (Errno::Einval)