使用 python-maas-client 进行 POST 请求

使用 python-maas-client 进行 POST 请求

我们如何使用 python-maas-client 向 maas api 发出发布请求。

我通过以下代码使用 maas-client 从 maas api 获取详细信息。

print maas_access.get(u"networks").read()
print maas_access.get(u"users").read()

但是当我尝试发布请求时出现错误。代码如下

data = {'username':'murali','email':'[email protected]','password':'murali','is_superuser': 0}
maas_access.post(u"users/", "new", data)
Traceback (most recent call last):
  File "api.py", line 48, in <module>
    maas_access.post(u"users/", "new", data)
  File "/usr/lib/python2.7/dist-packages/apiclient/maas_client.py", line 250, in post
    url, method="POST", headers=headers, data=body)
  File "/usr/lib/python2.7/dist-packages/apiclient/maas_client.py", line 116, in dispatch_query
    res = urllib2.urlopen(req)
  File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 410, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: BAD REQUEST

如果我使用

 data = {'username':'murali','email':'[email protected]','password':'murali','is_superuser': 0}
maas_access.post(u"users/", data)
Traceback (most recent call last):
  File "api.py", line 49, in <module>
    maas_access.post(u"users/", data)
  File "/usr/lib/python2.7/dist-packages/apiclient/maas_client.py", line 248, in post
    path, kwargs, as_json=as_json)
  File "/usr/lib/python2.7/dist-packages/apiclient/maas_client.py", line 218, in _formulate_change
    url += '?' + urlencode([('op', op)])
  File "/usr/lib/python2.7/dist-packages/apiclient/utils.py", line 48, in urlencode
    for name, value in data)
  File "/usr/lib/python2.7/dist-packages/apiclient/utils.py", line 48, in <genexpr>
    for name, value in data)
  File "/usr/lib/python2.7/dist-packages/apiclient/utils.py", line 45, in <lambda>
    string.encode("utf-8") if isinstance(string, unicode) else string)
  File "/usr/lib/python2.7/urllib.py", line 1295, in quote_plus
    return quote(s, safe)
  File "/usr/lib/python2.7/urllib.py", line 1286, in quote
    if not s.rstrip(safe):
AttributeError: 'dict' object has no attribute 'rstrip'

我是否遗漏了某些内容或我提供的数据有误?请帮助我

答案1

虽然没有关于 apiclient 当前形式的实际文档,但阅读 post() 方法签名的源代码:

def post(self, path, op, as_json=False, **kwargs):
    """Dispatch POST method `op` on `path`, with the given parameters.

    :param as_json: Instead of POSTing the content as multipart/form-data
        POST it as application/json
    :return: The result of the dispatch_query call on the dispatcher.
    """

这意味着您提供的任何数据都需要以 kwargs 的形式提交;如下所示:

maas_access.post(u"users/", "new", **data)

不过,您的数据还有一个问题,那就是您提交了一个整数作为参数;而是将其作为字符串“0”提交:

data = {'username':'murali', 'email':'[email protected]', 
     'password':'murali', 'is_superuser': '0'}
maas_access.post(u"users/", "new", **data)

最后,请注意,该 API 不安全,因为它以纯文本形式提供密码,如API 文档

http://blog.allenap.me/2013/06/workaround-for-uploading-files-to-maas.html以获得使用客户端 post() API 的更完整的示例。

答案2

sshKeys = {"key": [keyValue]}
maas_access.post(u"account/prefs/sshkeys/", "new", **sshKeys)

上述代码将使用 maas-api 将 ssh 密钥发布到 maas。但对于用户来说,它不起作用。仍然收到 400 错误

相关内容