Content-Type Negotiation and REST
This is a post from Luke's old blog; it is saved here statically for historical purposes, as of October 2008
I'm working on a few enhancements for the development branch in Puppet, and in particular trying to add support for negotiating content-type. Strangely, I can't find much evidence of people talking about doing this. Or rather, I can find lots of people talking about it, but not many real implementations.
Most people, including Rails, seem to take the easy way out of just using file extensions. I don't want to do this in Puppet because I think file extensions are essentially evil, since you're embedding metadata into your file name, but also, it doesn't work because we need to be able to transfer files directly -- if the file ends in .pdf, we want to be able to transfer it even though we don't necessarily know anything about PDFs.
So, I want to use the content-type header (and apparently the Accept header, for clients). I was hoping to just be able to crib on someone else's work, but I guess I can't.
Most of it's going to be pretty easy -- do a bit of metaprogramming to convert the type to methods (e.g., to_xml and from_xml). However, we need to be able to list supported types, so I've been trying to decide how to provide a list of supported types. I guess the easiest way is to continue with the metaprogramming and just convert back from those method names to a type name. E.g., if your class has to_xml, from_xml, to_json, and from_json methods, then it supports the json and xml formats.
Also, I should probably be using the word representation instead of format, but, well, that's a lot of typing. :)