我正在使用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
单独curl
来执行此操作。相反,我正在寻找一种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
可以处理此类请求吗?如果是,请纠正我的实施方式。