incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Samuel Wan <...@samuelwan.com>
Subject Re: specify Content-Type of a document?
Date Tue, 21 Apr 2009 20:56:37 GMT
Thanks a lot for the pointer, Paul, it would have taken a long time to
find otherwise. So lemme see if I got it right...

The manual approach 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>')
         }
- - - - - - -

The convenient approach is to use some global variables and helper
methods defined by CouchDB's <couchdb>/server/main.js file. One of
these methods is registerType, which lets you register a type key with
one or more MIME type strings.

- - - - - - -
registerType("foo", "application/foo", "application/x-foo"); //<--
stored to some global associative array defined by main.js
- - - - - - -

The other helper method is respondsWith, which accepts a second
argument (a key-value object) that maps type keys to functions that
return different kinds of HTTP responses depending on the type.

- - - - - - -
return respondWith(req, {
          html : function() {
            return {
              body:"Ha ha, you said \"" + doc.word + "\"."
            };
          },
          foo : function() {
            return {
              body: "foofoo"
            };
          },
          fallback : "html"
        });
- - - - - - -

So respondWith helper makes it easier for a show function respond to
different Content-Type request headers... i think... Since there's a
test for it, I guess content-type handling in the server-side JS is
something CouchDB intends to support moving forward...

-Sam


On Tue, Apr 21, 2009 at 12:57 PM, Paul Davis
<paul.joseph.davis@gmail.com> wrote:
> Sam,
>
> This sounds very much like you want a _show function. Both _show and
> _list can specify the content-type returned to the client. You'll want
> to check the test suite code and look for the respondWith stuff for
> examples.
>
> HTH,
> Paul Davis
>
> On Tue, Apr 21, 2009 at 3:51 PM, Samuel Wan <sam@samuelwan.com> wrote:
>> I'm trying to build a simple learning experiment where I can write
>> HTML and Javascript into documents, and retrieve them with a GET
>> request. However, I don't know how to specify text/html or
>> application/x-javascript as the Content-Type headers in the response
>> to a document request.
>>
>> Is the recommended practice to simply store the html or js text as
>> attachments, or is it possible somehow to use a "show" function to
>> send them back with specific content type headers? I read some of
>> Chris Anderson's posts, the Safari Rough Cuts book, and looked a bit
>> at the CouchApp code, but it might be too much to grasp all at once
>> for me.
>>
>> -Sam
>>
>> On Tue, Apr 21, 2009 at 12:33 PM, Paul Davis
>> <paul.joseph.davis@gmail.com> wrote:
>>> Sam,
>>>
>>> We're consistently inconsistent in that we only sometimes check for
>>> the content-type when posting JSON documents. Of the top of my head I
>>> know we check in _temp_views but not for PUTs and POSTs to docs or
>>> _bulk_docs.
>>>
>>> If you mean for adding attachments to docs though CouchDB will just
>>> send you back the content-type header you attached it with so you can
>>> control what clients will see when fetching attachments.
>>>
>>> HTH,
>>> Paul Davis
>>>
>>> On Tue, Apr 21, 2009 at 3:28 PM, Samuel Wan <sam@samuelwan.com> wrote:
>>>> Is it possible to specify the Content-Type header of a document? Do
>>>> you need to use a "show" function?
>>>>
>>>> -Sam
>>>>
>>>
>>
>

Mime
View raw message