Summary
Rack middleware to specify an app’s supported media types. Returns ‘406 Not Acceptable’ status when unsuported type is requested.
Install
gem install rack-supported-media-types
Example
require 'rack'
require 'rack/supported_media_types'
use Rack::SupportedMediaTypes, %w( application/xml application/json )
run App.new
# will give you:
GET /foo
Accept: text/html,text/plain
#=> 406 Not Acceptable
GET /foo
Accept: application/xml,text/html
#=> 200 OK
GET /foo
Accept: application/json,text/html
#=> 200 OK
see examples/simple.ru for example code.
Tip
To read the requested type from the url’s extension instead of the Accept header (which is a more realistic use case), use Rack::AbstractFormat before Rack::SupportedMediaTypes.
# gem install rack-abstract-format
require 'rack'
require 'rack/abstract_format'
require 'rack/supported_media_types'
use Rack::AbstractFormat
use Rack::SupportedMediaTypes, %w( application/xml application/json )
run App.new
# will result in:
GET /foo.html
#=> 406 Not Acceptable
GET /foo.xml
#=> 200 OK
GET /foo.json
#=> 200 OK
see examples/recommended.ru for example code.