4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Sinatra/Padrinoで特殊なMime Typeを登録する

Last updated at Posted at 2013-02-06

mime_type というクラスマクロがあります。

以上、よろしくお願いします。

* * *

これだけではあれなのでコードサンプルでも

app.rb
require 'sinatra'
require 'json'
require 'msgpack'

configure do
  mime_type :msgpack, "application/x-msgpack"
end

get '/hello.?:format?', :provides => [:json] do
  JSON.dump({hello: "world", data: [1, 2, 3, 99]})
end

get '/hello.?:format?', :provides => [:msgpack] do
  {hello: "world", data: [1, 2, 3, 99]}.to_msgpack
end
$ ruby app.rb &
$ curl -H "Accept: application/json" http://localhost:4567/hello.json
{"hello":"world","data":[1,2,3,99]}

$ curl -H "Accept: application/x-msgpack" http://localhost:4567/hello.msgpack
��hello�world�data�

$ curl -H "Accept: text/html" http://localhost:4567/hello.json
#=> Sinatra doesn't know this ditty!

Padrino なら

config/apps.rb

config/apps.rb
Padrino.configure_apps do
  #...
  mime_type :msgpack, "application/x-msgpack"
end

こんなんが良さそう。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?