incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alexander Shorin <kxe...@gmail.com>
Subject Re: Redirects from _list functions in CouchDB 1.4
Date Wed, 18 Sep 2013 03:50:24 GMT
Hi Bernhard,

you need to send redirect with start() function call.

function(head, req) {
  start({"code": 301, "headers": {"Location": "http://google.com"}});
}

http://docs.couchdb.org/en/1.4.x/query-servers.html

Why so? start() function allows to send custom HTTP headers to the
client before any response (or with first chunk of it). When you
return or send() something before or without start() call, query
server implicitly called it with empty object payload which means that
CouchDB will send default HTTP status code and headers. Since CouchDB
sends response of list functions as chunked transfer, it doesn't
buffers all of it to let you change HTTP response status or headers
somewhere in the middle - what's why you need to define them in first
place and start() function explicitly points on this.

For show functions your code will work since they produces only one
response object which contains response body and optionally may have
HTTP status and headers. With a little adaptation, the same is true
for update functions too.

--
,,,^..^,,,


On Tue, Sep 17, 2013 at 5:34 PM, Bernhard Gschwantner
<bernhard@unserwein.at> wrote:
> Hello,
>
> I tried to redirect from a _list function without success. This code throws
> a badarg error in CouchDB 1.4.0 (also in 1.2 and 1.3):
>
> function(head, req) {
>   return {
>     code : 301,
>     headers : {
>       Location : 'http://www.google.com'
>     }
>   };
> }
>
> Is there something I am doing wrong or missing?
>
> The real-world use case is that we have several aliases to each document in
> the db, and all those aliases should be redirected to the real document id.
> We use a view to emit the aliases and a list function to redirect all those
> aliases to the real url.
>
> We've been working around the previous issues with modifying headers after
> getRow() by redirecting through JavaScript, but want to get rid of this
> workaround eventually.
>
> Regards,
> Bernhard
>
> --
>
> Bernhard Gschwantner
> Unser Wein G&U OG
> Kirchengasse 13/7, 1070 Wien
>
> mobil: +43 (6991) 971 32 96
> tel: +43 (1) 971 32 95
> e-mail: bernhard@unserwein.at
> twitter: @bernharduw <http://twitter.com/bernharduw>
> web: www.unserwein.at

Mime
View raw message