尝试使用_find时,couchdb中的“Referer Header Required”错误

时间:2018-04-08 17:13:54

标签: macos curl couchdb

我开始使用CouchDB,并开始构建应用程序。我可以使用CURL编写和阅读,但我已经开始尝试搜索,使用_find,我得到一个错误,指出“Referer Header Required”。

curl -X POST http://localhost:5984/mydb/_find -d '{"selector":{"member.email":"foo@bar.com"}}

返回:

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

当我尝试在调用中添加“Referer”主机时,它只是说引用者需要匹配主机,但我不知道它的主机意味着什么。

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."}

我尝试了各种组合,使用localhost,有和没有端口号,我尝试过使用X-Forwarded-Host,我找到了十几篇关于POST与PUT和版本控制的相关文章,但我走到了尽头,只是无法弄清楚我错过了什么。

NB。我在OSX笔记本电脑上运行couchdb1.7.x.

2 个答案:

答案 0 :(得分:1)

我通过查看源代码设法修复了 Referer header required 和后续的 Referer header must match host。虽然真正的问题是期望 _find 在那些古老的 1.X 构建操作系统包管理器中工作。最好升级到最新版本的 couchdb (3.1.1)。

您可以通过将虚拟主机添加到配置文件 /etc/couchdb/local.ini

来解决旧版本的错误
[vhosts]
;localhost = /db/
localhost = /*
[httpd]
x_forwarded_host = Y-Forwarded-Host

并且可能还为 X-Forwarded-Host 使用自定义标头名称,在我的情况下,获取 API 调用的软件对 X-Forwarded-Host 和 Host 标头做了一些过于聪明的事情。

因此,当您调用 couchdb 时,只需包含带有 'localhost' 的新标头

curl -X POST http://localhost:5984/mydb/_find \
-H 'Referer: localhost' \
-H 'Y-Forwarded-Host: localhost' \
-d '{"selector":{"member.email":"foo@bar.com"}}

或空白以便匹配

curl -X POST http://localhost:5984/mydb/_find \
-H 'Referer' \
-H 'X-Forwarded-Host' \
-d '{"selector":{"member.email":"foo@bar.com"}}

在版本 2 或 3 以后的版本中都不需要标题,他们意识到了。

答案 1 :(得分:0)

可能为时已晚,但对于接下来要讨论此主题的人(例如我), 问题是:

http://localhost:5984/mydb/_find

当您尝试在PUT中更改POST时,您会遇到问题:

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

_document是指特殊功能,因此只需更改文档名称即可:)

(应该在这两个帖子中/之后都应该起作用)

相关问题