How can we do the post request to maas api using the python-maas-client.

I am getting the details from maas api using the maas-client by the following code.

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

But when i try to post a request I am getting the error. code is below

data = {'username':'murali','email':'muralidharan.s@poornam.com','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

and if i Use

 data = {'username':'murali','email':'muralidharan.s@poornam.com','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'

Is I am missing some thing or the data that i providing is wrong ? Please help me

share|improve this question

While there is no actual documentation for the current form of the apiclient, reading the source for the signature of the post() method:

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

That suggests that any data you provide needs to be submitted as kwargs; as below:

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

There is one more problem with your data, though, which is that you are submitting an integer as an argument; instead, submit it as the string '0':

data = {'username':'murali', 'email':'muralidharan.s@poornam.com', 
     'password':'murali', 'is_superuser': '0'}
maas_access.post(u"users/", "new", **data)

Finally, note that that API is insecure as it supplies the password as plaintext, as indicated in the API documentation.

See http://blog.allenap.me/2013/06/workaround-for-uploading-files-to-maas.html for a more complete example of using the client's post() API.

share|improve this answer
    
Hi kiko thanks for the reply. We are getting another error now . paste.ubuntu.com/12514596 – binoy Sep 21 '15 at 14:41
    
I have tried with the following data = {'username':names, 'email':emailss, 'password':passwds, 'is_superuser': sup} maas_access.post(u"users/", "create", **data) maas_access.post(u"users/", "POST", **data) maas_access.post(u"users/", "None", **data) maas_access.post(u"users/", "new", **data) maas_access.post(u"users/", "add", **data) Getting the following error raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) urllib2.HTTPError: HTTP Error 400: BAD REQUEST – binoy Sep 22 '15 at 11:00
sshKeys = {"key": [keyValue]}
maas_access.post(u"account/prefs/sshkeys/", "new", **sshKeys)

The above code will post the ssh key into the maas using maas-api. But in case of user it is not working. Still getting 400 error

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.