Skip to content
Permalink
Browse files

Fix #317: Added headers that returns a hash of parsed HTTP request he…

…aders.
  • Loading branch information...
dblock committed Feb 7, 2013
1 parent 1a54559 commit f6f585ea6cc720e779a37f756b9e9cda4786dad2
Showing with 35 additions and 6 deletions.
  1. +2 −1 CHANGELOG.markdown
  2. +3 −5 README.markdown
  3. +11 −0 lib/grape/endpoint.rb
  4. +19 −0 spec/grape/endpoint_spec.rb
@@ -6,7 +6,8 @@ Next Release
* [#309](https://github.com/intridea/grape/pull/309): An XML format API will return an error instead of returning a string representation of the response if the latter cannot be converted to XML - [@dblock](http://github.com/dblock).
* [#309](https://github.com/intridea/grape/pull/309): Added XML support to entities - [@johnnyiller](https://github.com/johnnyiller), [@dblock](http://github.com/dblock).
* A formatter that raises an exception will cause the API to return a 500 error - [@dblock](http://github.com/dblock).
* [#131](https://github.com/intridea/grape/issues/131) Added instructions for Grape API reloading in Rails - [@jyn](http://github.com/jyn), [@dblock](http://github.com/dblock).
* [#131](https://github.com/intridea/grape/issues/131): Added instructions for Grape API reloading in Rails - [@jyn](http://github.com/jyn), [@dblock](http://github.com/dblock).
* [#317](https://github.com/intridea/grape/issues/317): Added `headers` that returns a hash of parsed HTTP request headers - [@dblock](http://github.com/dblock).
* Your contribution here.

0.2.6 (01/11/2013)
@@ -363,23 +363,21 @@ end

## Headers

Headers are available through the `header` helper or the `env` hash object.
Request headers are available through the `headers` helper or from `env` in their original form.

```ruby
get do
content_type = header['Content-type']
# ...
error!('Unauthorized', 401) unless headers['Secret-Password'] == 'swordfish'
end
```

```ruby
get do
error!('Unauthorized', 401) unless env['HTTP_SECRET_PASSWORD'] == 'swordfish'
# ...
end
```

You can set a header with `header` inside an API call.
You can set a response header with `header` inside an API.

```ruby
header "X-Robots-Tag", "noindex"
@@ -239,6 +239,17 @@ def header(key = nil, val = nil)
end
end

# Retrieves all available request headers.
def headers
@headers ||= @env.dup.inject({}) { |h, (k, v)|
if k.start_with? 'HTTP_'
k = k[5..-1].gsub('_', '-').downcase.gsub(/^.|[-_\s]./) { |x| x.upcase }
h[k] = v
end
h
}
end

# Set response content-type
def content_type(val)
header('Content-Type', val)
@@ -47,6 +47,25 @@ def app; subject end
end
end

describe '#headers' do
before do
subject.get('/headers') do
headers.to_json
end
end
it 'includes request headers' do
get '/headers'
JSON.parse(last_response.body).should == {
"Host" => "example.org",
"Cookie" => ""
}
end
it 'includes additional request headers' do
get '/headers', nil, { "HTTP_X_GRAPE_CLIENT" => "1" }
JSON.parse(last_response.body)["X-Grape-Client"].should == "1"
end
end

describe '#cookies' do
it 'is callable from within a block' do
subject.get('/get/cookies') do

2 comments on commit f6f585e

@cobbr2

This comment has been minimized.

Copy link

replied Feb 7, 2013

Looks good! Thanks, dB; wish I'd gotten to it myself.

@zolzaya

This comment has been minimized.

Copy link

replied Feb 13, 2013

+1

Please sign in to comment.
You can’t perform that action at this time.