JSON 对象{"name": "newname"}
需要作为请求正文传递。新名称不能作为 URL 路径参数传递。问题是实施collection.action()
:
def action(self, method, action, **params) :
"a generic fct for interacting everything that doesn't have an assigned fct"
fct = getattr(self.connection.session, method.lower())
r = fct(self.URL + "/" + action, params = params)
return r.json()
关键字参数以 dict 的形式结束params
。fct()
该对象作为命名参数传递给请求函数params
。该参数接收dict并将其转换为URL路径参数,例如?name=newname
服务器的HTTP API不支持。
不幸的是,没有办法通过action()
. 但是,您可以编写一些自定义代码:
from pyArango.connection import *
connection = Connection('http://localhost:8529', username='root', password='')
try:
connection.createDatabase('test-db')
except CreationError:
pass
test_db = Database(connection, 'test-db')
try:
test_db.createCollection(name='new')
except CreationError:
pass
collection = test_db['new']
r = connection.session.put(collection.URL + '/rename', data='{"name":"newname"}')
print(r.text)
collection = test_db['newname']
如果需要,您还可以将 dict 用于有效负载并将其转换为 JSON:
import json
...put(..., data=json.dumps({"name": "newname"}))