Skip to content

Commit

Permalink
Merge pull request #202 from dblock/override-content-type
Browse files Browse the repository at this point in the history
Implements #97: allow overriding Content-Type.
  • Loading branch information
dblock committed Jul 11, 2012
2 parents 2574a89 + bfa86c0 commit 989974a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Next Release
* [#156](https://github.com/intridea/grape/pull/156): Added support for adding formatters to entities - [@bobbytables](https://github.com/bobbytables).
* [#183](https://github.com/intridea/grape/pull/183): Added ability to include documentation in entities - [@flah00](https://github.com/flah00).
* [#189](https://github.com/intridea/grape/pull/189): `HEAD` requests no longer return a body - [@stephencelis](https://github.com/stephencelis).
* [#97](https://github.com/intridea/grape/issues/97): Allow overriding `Content-Type` - [@dblock](https://github.com/dblock).

0.2.0 (3/28/2012)
=================
Expand Down
27 changes: 19 additions & 8 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ end

## Mounting

The above sample creates a Rack application that can be run from a rackup *config.ru* file
The above sample creates a Rack application that can be run from a rackup *config.ru* file
with `rackup`:

``` ruby
Expand Down Expand Up @@ -128,7 +128,7 @@ There are three strategies in which clients can reach your API's endpoints: `:he
version 'v1', :using => :header
```

Using this versioning strategy, clients should pass the desired version in the HTTP Accept head.
Using this versioning strategy, clients should pass the desired version in the HTTP Accept head.

curl -H Accept=application/vnd.twitter-v1+json http://localhost:9292/statuses/public_timeline

Expand All @@ -147,15 +147,15 @@ Using this versioning strategy, clients should pass the desired version in the U

curl -H http://localhost:9292/v1/statuses/public_timeline

Serialization takes place automatically.
Serialization takes place automatically.

### Param

```ruby
version 'v1', :using => :param
```

Using this versioning strategy, clients should pass the desired version as a request parameter, either in the URL query string or in the request body.
Using this versioning strategy, clients should pass the desired version as a request parameter, either in the URL query string or in the request body.

curl -H http://localhost:9292/events?apiver=v1

Expand All @@ -169,7 +169,7 @@ version 'v1', :using => :param, :parameter => "v"

## Parameters

Parameters are available through the `params` hash object. This includes `GET` and `POST` parameters,
Parameters are available through the `params` hash object. This includes `GET` and `POST` parameters,
along with any named parameters you specify in your route strings.

```ruby
Expand Down Expand Up @@ -426,9 +426,20 @@ class Twitter::API < Grape::API
end
```

You can override the content-type by setting the `Content-Type` header.

``` ruby
class API < Grape::API
get '/script' do
content_type "application/javascript"
"var x = 1;"
end
end
```

## Writing Tests

You can test a Grape API with RSpec by making HTTP requests and examining the response.
You can test a Grape API with RSpec by making HTTP requests and examining the response.

### Writing Tests with Rack

Expand Down Expand Up @@ -579,7 +590,7 @@ end
### Caveats

Entities with duplicate exposure names and conditions will silently overwrite one another.
In the following example, when object#check equals "foo", only afield will be exposed.
In the following example, when object#check equals "foo", only afield will be exposed.
However, when object#check equals "bar" both bfield and foo will be exposed.

```ruby
Expand Down Expand Up @@ -611,7 +622,7 @@ end

Grape lets you add a description to an API along with any other optional
elements that can also be inspected at runtime.
This can be useful for generating documentation. If the response
This can be useful for generating documentation. If the response
requires documentation, consider using an entity.

``` ruby
Expand Down
7 changes: 6 additions & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ def header(key = nil, val = nil)
@header
end
end


# Set response content-type
def content_type(val)
header('Content-Type', val)
end

# Set or get a cookie
#
# @example
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/middleware/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def after
bodymap = bodies.collect do |body|
formatter.call(body)
end
headers['Content-Type'] = content_types[env['api.format']]
headers['Content-Type'] = content_types[env['api.format']] unless headers['Content-Type']
Rack::Response.new(bodymap, status, headers).to_a
end
end
Expand Down
12 changes: 10 additions & 2 deletions spec/grape/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,12 +731,20 @@ def three
describe ".content_type" do
it "sets additional content-type" do
subject.content_type :xls, "application/vnd.ms-excel"
subject.get(:hello) do
subject.get :excel do
"some binary content"
end
get '/hello.xls'
get '/excel.xls'
last_response.content_type.should == "application/vnd.ms-excel"
end
it "allows to override content-type" do
subject.get :content do
content_type "text/javascript"
"var x = 1;"
end
get '/content'
last_response.content_type.should == "text/javascript"
end
end

describe ".default_error_status" do
Expand Down

0 comments on commit 989974a

Please sign in to comment.