Question

How can I replay a header which is sent as request to couch db, catch is I want to do it for all documents of data base irrespective of show functions.

So I would send a request to couch db for creating a document as

 curl -X POST -i -H "Content-Type: application/json" -H "User-Agent: CURL" --data-binary          @${INPUTFILE}  http://someip:port/db

where db is my database name created already in couch db and input file is json file to be sent to couch db which will be stored.

Here the response is always as follows

HTTP/1.1 201 Created
Server: CouchDB/1.5.0 (Erlang OTP/R16B01)
Location: http://someip:port/db/14f49d6e537268402761d873560b1da9^M
ETag: "1-ad9ad86f3ca9b8671cd0fcb9e546971f"
Date: Mon, 10 Mar 2014 19:41:07 GMT^M
Content-Type: text/plain; charset=utf-8
Content-Length: 95
Cache-Control: must-revalidate^M

{"ok":true,"id":"14f49d6e537268402761d873560b1da9","rev":"1-ad9ad86f3ca9b8671cd0fcb9e546971f"}

Now I need get an additional header in the response. I know we can easily do this by showfunctions, but the catch there is I have to send post request to a url of that show function similar to as follows

http://someip:port/db/_design/mydesigndoc/_show/myshowfunction

But I don't want this, I want to know whether it would be possible to just post to http://someip:port/db/ and get a custom response header back?

Or is there a way where I can front end the address to http://someip:port/db and forward it to http://someip:port/db/_design/mydesigndoc/_show/myshowfunction when ever I receive a request? I guess this should be possible, but couldn't figure out.

Any help would be greatly appreciated. Thanks

Was it helpful?

Solution 2

Perhaps the virtual hosts and/or rewriting documentation will help:

http://docs.couchdb.org/en/latest/config/http.html#virtual-hosts

OTHER TIPS

Finally came up with a solution, we suits my use case. Although I lose the headers generated by Couch in response, I'm ok with that for my case.

The update handlers, Rewrite URL, Show function and vhosts combination helped me.

As mentioned in my problem statement, the url I want to hit is

http://someip:port/dbname?version=1

If I were to have liberty to change this, I would have solved this problem with less effort. But this is the URL that will be hit with a POST request for creating a document in couch DB.

There are two solutions, one create the dbname as different If you can do that, and redirect this URL to the update handler URL in the rewrite or directly using vhosts.

If you cannot change dbname to a different thing, the problem is more complicated, if you put

http://someip:port/dbname?version=1

in the vhosts and direct it to update handler, then you cannot access the db from Futon UI as get request from Futon UI would direct to update handler and HTTP method mismatch error is thrown.

To overcome this, create a rewrite handler for directing requests coming from

someip:port/dbname?version=1

to update handler, and requests coming to

someip:port/dbname

to a show function, which shows the default view as seen from Futon.

Best solution would be, if you have liberty to change DB name then redirect the URL to the DB design doc's update handler for POST.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top