You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by Apache Wiki <wi...@apache.org> on 2009/04/22 07:48:53 UTC
[Couchdb Wiki] Update of "Formatting with Show and List" by SamuelWan
Dear Wiki user,
You have subscribed to a wiki page or wiki category on "Couchdb Wiki" for change notification.
The following page has been changed by SamuelWan:
http://wiki.apache.org/couchdb/Formatting_with_Show_and_List
The comment on the change is:
Added sections about dealing with content-types manually or via respondWith()
------------------------------------------------------------------------------
}
}}}
+ === Specifying Content-Type Response Header ===
+ There are two ways to deal with a content-type header in the response to a show or list request. The first way is to specify the content type as a member of the _show function's response object:
+
+ {{{
+ return {
+ "headers" : {"Content-Type" : "application/xml"},
+ "body" : new XML('<xml><node foo="bar"/></xml>')
+ }
+ }}}
+
+
+ === Responding to different Content-Type Request Headers ===
+ The second way to deal with content-type headers is to rely on some global helper
+ methods defined by CouchDB's ''<couchdb>/server/main.js'' file. The ''registerType'' method lets you register a type key with
+ one or more content-type strings. Please refer to the ''main.js'' file to see content-types registered by default.
+
+ {{{
+ registerType("foo", "application/foo", "application/x-foo");
+ }}}
+
+ The other global helper method for handling varying Content-Type headers is ''respondWith''. This helper method allows you to specify different response objects depending on the type key that corresponds to the content-type request header. The first argument is the request object, and the second argument is a key-value object that maps type keys to functions. Each function is expected to return an HTTP response object customized for the requested Content-Type.
+
+ {{{
+ //... in your show function...
+ return respondWith(req, {
+ html : function() {
+ return {
+ body:"Ha ha, you said \"" + doc.word + "\"."
+ };
+ },
+ foo : function() {
+ return {
+ body: "foofoo"
+ };
+ },
+ fallback : "html"
+ });
+ }}}
+
+
Hopefully this is enough to get started. For a more complete set of examples, see the CouchDB test suite, especially show_documents.js and list_views.js