Matthew Knight
Matthew Knight

Reputation: 631

"Referer Header Required" error in couchdb when trying to use _find

I'm getting started with CouchDB, and have started building an application. I can write and read just fine using CURL, but i've started to try and search, using _find, and i get an error stating "Referer Header Required".

curl -X POST http://localhost:5984/mydb/_find -d '{"selector":{"member.email":"[email protected]"}}

returns:

{"error":"bad_request","reason":"Referer header required."}

When I try and add a "Referer" host in the call, it just says the referer needs to match the host, but I don't know what host it means.

curl -X POST http://localhost:5984/mydb/_find -d '{"selector":{"member.email":"matthew"}}' -H "Referer: localhost" {"error":"bad_request","reason":"Referer header must match host."}

I've tried all sorts of combinations, using the localhost, the with and without the port numbers, I've tried with X-Forwarded-Host, I've find a dozen related articles about POST vs PUT, and versioning, but I've come to a dead end, and just can't figure out what I'm missing.

nb. I'm running couchdb1.7.x on an OSX laptop.

Upvotes: 8

Views: 5696

Answers (2)

bumkino
bumkino

Reputation: 31

I managed to fix the Referer header required and the subsequent Referer header must match host by looking through the source code. Though the real problem was expecting _find to work in those ancient 1.X builds OS package managers default to. It's much better to upgrade to the latest version of couchdb (3.1.1).

You can workaround the error on old versions by adding vhosts to the config file /etc/couchdb/local.ini

[vhosts]
;localhost = /db/
localhost = /*
[httpd]
x_forwarded_host = Y-Forwarded-Host

and potentially also using a custom header name for X-Forwarded-Host, in my case the software fetching the API call does some overly clever things to X-Forwarded-Host and Host headers.

so when you call couchdb just include the new header with either 'localhost'

curl -X POST http://localhost:5984/mydb/_find \
-H 'Referer: localhost' \
-H 'Y-Forwarded-Host: localhost' \
-d '{"selector":{"member.email":"[email protected]"}}

or blank so they match

curl -X POST http://localhost:5984/mydb/_find \
-H 'Referer' \
-H 'X-Forwarded-Host' \
-d '{"selector":{"member.email":"[email protected]"}}

neither header is needed in version 2 or 3 onward, they came to their senses.

Upvotes: 3

vincplaying
vincplaying

Reputation: 21

Probaly too late but for for the next people (like me) that will get on this topic, the problem is :

http://localhost:5984/mydb/_find

When you try to change POST in PUT you get the problem :

"reason":"Only reserved document ids may start with underscore."

the _document refer to special function so just change the name of the document :)

(should work in both post / put after this)

Upvotes: 1

Related Questions