Rendering JSON from Sinatra
I’ve updated this post for Sinatra 0.10.1
Sinatra makes amazing web service endpoints. The lightweight “nothing in your way” approach is great. Rendering alternate media types isn’t immediately obvious though. Here’s a little snippet that demonstrates rendering JSON. XML would work in a very similar way.
December 18th, 2008 at 12:09 am
You’re not kidding it’s great. A couple of us have been building quite a few projects this way. Evoke ( http://github.com/thumblemonks/evoke/tree/master ) is but one of those.
We also started a simple sinatra extensions project called Chicago ( http://github.com/thumblemonks/chicago/tree/master ) that has some support for JSON now (including some shoulda macros).
- gus
December 23rd, 2008 at 8:43 pm
Chris, I think I found it: it’s #mime, not #mime_type.
Thanks for the tip!
April 17th, 2009 at 6:11 pm
This code fragment is very high for json and sinatra so I think it’s worth listing the requires too:
require ‘rubygems’
require ‘sinatra’
require ‘json’
mime :json, “application/json”
….
April 17th, 2009 at 6:12 pm
Very high “on google” that was supposed to be!