Skip to content

Commit

Permalink
Fix #317: Added headers that returns a hash of parsed HTTP request he…
Browse files Browse the repository at this point in the history
…aders.
  • Loading branch information
dblock committed Feb 7, 2013
1 parent 1a54559 commit f6f585e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 3 additions & 5 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions spec/grape/endpoint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit f6f585e

@cobbr2
Copy link

@cobbr2 cobbr2 commented on f6f585e Feb 7, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@zolzaya
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Please sign in to comment.