我正在使用 couchdb 处理更新处理程序,并面临这个奇怪的问题:
这是我的更新处理程序:
"updates": {
"temp": "function(doc, req) {var inc_amount =JSON.parse(req.query.amount);doc[1]=inc_amount;return [doc, JSON.stringify(inc_amount) + JSON.stringify({\"STATUS\": \"message\"})];}"
},
这是我的 couchdb 文档:
{
"_id": "my_doc",
"_rev": "51-62e63d1ba46abbe6678a6c57f5500782"
}
我想用amount
查询参数的值更新这个文档。以下是我如何使用命令行(基本上是 curl)来做到这一点:
curl -X PUT http://localhost:5984/db/_design/designdoc/_update/temp/my_doc?amount='\{"a":\{"b":"c"\}\}'
这个命令非常适合我。但是,由于此更新将由 python 代码调用,因此我不想要单独subprocess
的 forcurl
来执行此操作。相反,我正在寻找一个 pythonic 解决方案。这是我尝试使用requests模块实现的目标:
>>> import json
>>> d={'a':{'b':'c'}}
>>> d
{'a': {'b': 'c'}}
>>> s=json.dumps(d)
>>> s
'{"a": {"b": "c"}}'
>>> import requests
>>> r=requests.put('http://localhost:5984/file_status_collector/_design/reportsDesignDoc/_update/temp/my_doc', data = {'amount': s})
>>> r.status_code
500
>>> r.text
u'{"error":"render_error","reason":"function raised error: (new SyntaxError(\\"JSON.parse\\", \\"/opt/local/share/couchdb/server/main.js\\", 481)) \\nstacktrace: SyntaxError(\\"JSON.parse\\")@:0\\n(\\"undefined\\")@/opt/local/share/couchdb/server/main.js:481\\n([object Object],[object Object])@:0\\napply([object Object],[object Array])@:0\\nrunUpdate(function (doc, req) {var inc_amount = JSON.parse(req.query.amount);doc[1] = inc_amount;return [doc, JSON.stringify(inc_amount) + JSON.stringify({STATUS:\\"message\\"})];},[object Object],[object Array])@/opt/local/share/couchdb/server/main.js:952\\n(function (doc, req) {var inc_amount = JSON.parse(req.query.amount);doc[1] = inc_amount;return [doc, JSON.stringify(inc_amount) + JSON.stringify({STATUS:\\"message\\"})];},[object Object],[object Array])@/opt/local/share/couchdb/server/main.js:1021\\napply(null,[object Array])@:0\\n(\\"_design/reportsDesignDoc\\",[object Array],[object Array])@/opt/local/share/couchdb/server/main.js:1492\\napply(null,[object Array])@:0\\n()@/opt/local/share/couchdb/server/main.js:1535\\n@/opt/local/share/couchdb/server/main.js:1546\\n"}\n'
在这里我遇到了一个问题。因此,我想将 json 字符串更改为s
我提供给 curl 的格式:
>>> s=s.replace(' ','').replace('{','\{').replace('}','\}')
>>> s
'\\{"a":\\{"b":"c"\\}\\}'
>>> print s
\{"a":\{"b":"c"\}\}
>>> r=requests.put('http://localhost:5984/file_status_collector/_design/reportsDesignDoc/_update/temp/my_doc', data = {'amount': s})
>>> r.status_code
500
>>> r.text
u'{"error":"render_error","reason":"function raised error: (new SyntaxError(\\"JSON.parse\\", \\"/opt/local/share/couchdb/server/main.js\\", 481)) \\nstacktrace: SyntaxError(\\"JSON.parse\\")@:0\\n(\\"undefined\\")@/opt/local/share/couchdb/server/main.js:481\\n([object Object],[object Object])@:0\\napply([object Object],[object Array])@:0\\nrunUpdate(function (doc, req) {var inc_amount = JSON.parse(req.query.amount);doc[1] = inc_amount;return [doc, JSON.stringify(inc_amount) + JSON.stringify({STATUS:\\"message\\"})];},[object Object],[object Array])@/opt/local/share/couchdb/server/main.js:952\\n(function (doc, req) {var inc_amount = JSON.parse(req.query.amount);doc[1] = inc_amount;return [doc, JSON.stringify(inc_amount) + JSON.stringify({STATUS:\\"message\\"})];},[object Object],[object Array])@/opt/local/share/couchdb/server/main.js:1021\\napply(null,[object Array])@:0\\n(\\"_design/reportsDesignDoc\\",[object Array],[object Array])@/opt/local/share/couchdb/server/main.js:1492\\napply(null,[object Array])@:0\\n()@/opt/local/share/couchdb/server/main.js:1535\\n@/opt/local/share/couchdb/server/main.js:1546\\n"}\n'
仍然,没有帮助,现在,我放下了我的手臂。不知道我应该如何以pythonic的方式做到这一点。这里绝对需要你的帮助。你能帮我解决以下问题吗:
- 我
requests.put
是否正确使用了json?可以requests.put
处理这样的请求吗?如果是,请更正我的实施方式。