Skip to content

Commit 4d01406

Browse files
committed
Implements ruby-grape#97: allow overriding Content-Type.
1 parent 575e6fc commit 4d01406

File tree

5 files changed

+39
-14
lines changed

5 files changed

+39
-14
lines changed

CHANGELOG.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Next Release
1111
* [#159](https://github.com/intridea/grape/pull/159): Added `:requirements` to routes, allowing to use reserved characters in paths - [@gaiottino](https://github.com/gaiottino).
1212
* [#156](https://github.com/intridea/grape/pull/156): Added support for adding formatters to entities - [@bobbytables](https://github.com/bobbytables).
1313
* [#183](https://github.com/intridea/grape/pull/183): Added ability to include documentation in entities - [@flah00](https://github.com/flah00).
14+
* [#97](https://github.com/intridea/grape/issues/97): Allow overriding `Content-Type` - [@dblock](https://github.com/dblock).
1415

1516
0.2.0 (3/28/2012)
1617
=================

README.markdown

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ end
8888

8989
## Mounting
9090

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

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

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

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

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

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

150-
Serialization takes place automatically.
150+
Serialization takes place automatically.
151151

152152
### Param
153153

154154
```ruby
155155
version 'v1', :using => :param
156156
```
157157

158-
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.
158+
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.
159159

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

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

170170
## Parameters
171171

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

175175
```ruby
@@ -426,9 +426,20 @@ class Twitter::API < Grape::API
426426
end
427427
```
428428

429+
You can override the content-type by setting the `Content-Type` header.
430+
431+
``` ruby
432+
class API < Grape::API
433+
get '/script' do
434+
content_type "application/javascript"
435+
"var x = 1;"
436+
end
437+
end
438+
```
439+
429440
## Writing Tests
430441

431-
You can test a Grape API with RSpec by making HTTP requests and examining the response.
442+
You can test a Grape API with RSpec by making HTTP requests and examining the response.
432443

433444
### Writing Tests with Rack
434445

@@ -579,7 +590,7 @@ end
579590
### Caveats
580591

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

585596
```ruby
@@ -611,7 +622,7 @@ end
611622

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

617628
``` ruby

lib/grape/endpoint.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def error!(message, status=403)
152152
end
153153

154154
# Redirect to a new url.
155-
#
155+
#
156156
# @param url [String] The url to be redirect.
157157
# @param options [Hash] The options used when redirect.
158158
# :permanent, default true.
@@ -163,7 +163,7 @@ def redirect(url, options = {})
163163
else
164164
if env['HTTP_VERSION'] == 'HTTP/1.1' && request.request_method.to_s.upcase != "GET"
165165
status 303
166-
else
166+
else
167167
status 302
168168
end
169169
end
@@ -197,7 +197,12 @@ def header(key = nil, val = nil)
197197
@header
198198
end
199199
end
200-
200+
201+
# Set response content-type
202+
def content_type(val)
203+
header('Content-Type', val)
204+
end
205+
201206
# Set or get a cookie
202207
#
203208
# @example

lib/grape/middleware/formatter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def after
7373
bodymap = bodies.collect do |body|
7474
formatter.call(body)
7575
end
76-
headers['Content-Type'] = content_types[env['api.format']]
76+
headers['Content-Type'] = content_types[env['api.format']] unless headers['Content-Type']
7777
Rack::Response.new(bodymap, status, headers).to_a
7878
end
7979
end

spec/grape/api_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -731,12 +731,20 @@ def three
731731
describe ".content_type" do
732732
it "sets additional content-type" do
733733
subject.content_type :xls, "application/vnd.ms-excel"
734-
subject.get(:hello) do
734+
subject.get :excel do
735735
"some binary content"
736736
end
737-
get '/hello.xls'
737+
get '/excel.xls'
738738
last_response.content_type.should == "application/vnd.ms-excel"
739739
end
740+
it "allows to override content-type" do
741+
subject.get :content do
742+
content_type "text/javascript"
743+
"var x = 1;"
744+
end
745+
get '/content'
746+
last_response.content_type.should == "text/javascript"
747+
end
740748
end
741749

742750
describe ".default_error_status" do

0 commit comments

Comments
 (0)