#30 √ resolved
Adam Wiggins

Accept header

Reported by Adam Wiggins | June 9th, 2008 @ 12:59 AM | in 0.9.0 Hoboken

Filter events on the Accept HTTP header. Example:

put '/photos/:id', :accept => :xml
  Photo.find(id).from_xml(request.body.string)
end

put '/photos/:id', :accept => 'image/jpg'
  Photo.find(id).save_image(request.body)
end

Patch #1: the basic feature

http://github.com/adamwiggins/si...

Patch #2: Factor out mimetype lookup into lookup_mime

http://github.com/adamwiggins/si...

Patch #3: Set the content-type

http://github.com/adamwiggins/si...

Comments and changes to this ticket

  • Blake Mizerany

    Blake Mizerany June 9th, 2008 @ 04:04 PM

    killer. I'll pull soon.

  • Adam Wiggins
  • Ryan Tomayko

    Ryan Tomayko September 7th, 2008 @ 06:29 PM

    • → Tag changed from “” to “feature http patch pull”
    • → Assigned user changed from “Blake Mizerany” to “Ryan Tomayko”
  • Ryan Tomayko

    Ryan Tomayko September 14th, 2008 @ 03:19 AM

    • → Milestone cleared.

    Pushing to 0.4. I'd like to keep new features on the 0.3.x series to a minimum to lessen the backward compatibility burden on redux/0.4. I believe the redux branch handles options like this a bit better as well.

  • Ryan Tomayko

    Ryan Tomayko December 30th, 2008 @ 02:46 AM

    • → Milestone changed from “” to “0.9.0 Hoboken”
    • → State changed from “new” to “hold”

    We absolutely have to add this to hoboken. The new condition stuff should make it fairly straightforward.

  • Ryan Tomayko

    Ryan Tomayko December 30th, 2008 @ 02:51 AM

    • → State changed from “hold” to “new”
  • Markus Prinz

    Markus Prinz January 6th, 2009 @ 02:56 PM

    • → State changed from “new” to “open”
    • → Assigned user changed from “Ryan Tomayko” to “Markus Prinz”
  • Markus Prinz

    Markus Prinz January 7th, 2009 @ 11:40 AM

    I've attached an updated patch that adds support for this in 0.9. It's also availabe on GitHub: http://github.com/cypher/sinatra...

    Comments/Reviews welcome.

  • Ryan Tomayko

    Ryan Tomayko January 7th, 2009 @ 12:49 PM

    This is a good start. I'm going to merge as is unless someone speaks up but I think we should discuss some potential refinements before closing the ticket.

    1. I don't want to "extend Helpers" in Sinatra::Base if we can find a cleaner way. Most of the methods in Helpers makes sense only in the context of a request and it's confusing to have them at the class level. I think there might also be some name conflicts here (like "error"). Let's move media_type to Base::media_type and call it from Helpers#media_type instead. That way it's available in both places.

    2. I could have swore there was some utility code for dealing with Accept headers in Rack but I can't find it -- they do have stuff for "Accept-Encoding" http://github.com/rack/rack/tree... but that doesn't help us :( The problem we're going to run into is that Accept headers often include quality values for each media type ("Accept: text/html;q=0.9, text/plain;q=0.5" ... more here: http://www.w3.org/Protocols/rfc2....1). This is going to screw up the matching logic. I don't think we should try to implement full-blown Accept parsing in Sinatra, though. Let me see if I can get something into Rack that we could use.

    3. We should kick around some alternative names for the "accept_mime" method and ":accept" option. The name "Accept" really only makes sense when you're the client. Names like "format", "type", "produces", "provides" might make more sense from the server's point of view. On the other hand, people with a good understanding of HTTP will find comfort in things being named similarly.

    Now that I think about it, see about fixing #1 and then I'll merge. We can iterate on these other issues afterward.

  • Markus Prinz

    Markus Prinz January 7th, 2009 @ 01:26 PM

    I think we should fix #3 too before merging.

    I'd probably go for either "accept_types" or "accept_mime_types" as method names, with a preference for the latter, since I find that to be clearer.

    As for the option name: ":provides" sounds good to me.

  • Markus Prinz

    Markus Prinz January 7th, 2009 @ 01:55 PM

    Attached is an updated patch with all the changes discussed.

  • Ryan Tomayko

    Ryan Tomayko January 7th, 2009 @ 03:16 PM

    • → State changed from “open” to “resolved”

    Okay, we still need to deal with the second issue at some point but I'm going to close this. Let's open another ticket for that.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »