POST request using python-maas-client

Bug #1496360 reported by Binoy M V on 2015-09-16
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MAAS
Undecided
Unassigned

Bug Description

Hi,

How can we do the post request to maas api using the python-maas-client.
I try to get the details from maas api usign 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.

data = ({
        "username": "testuser",
        "is_superuser": 1,
        "email": "<email address hidden>"
 });

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

Traceback (most recent call last):
  File "api.py", line 117, 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

Binoy M V (binoy-mv-s) wrote :

I tried with

data = {
        "username": "murali",
       "is_superuser": 1,
       "email": "<email address hidden>"
 }

Also getting same error.
]
Traceback (most recent call last):
  File "api.py", line 120, 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'

Gavin Panella (allenap) wrote :

Try with:

 data = {
       "username": "murali",
       "is_superuser": "1",
       "email": "<email address hidden>"
 }

I think it's choking on the integer.

Changed in maas:
status: New → Invalid
Binoy M V (binoy-mv) wrote :

No luck, getting same error from the code

data = {
       "username": "murali",
       "is_superuser": "1",
       "email": "<email address hidden>"
 }

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'

Changed in maas:
status: Invalid → New
Gavin Panella (allenap) wrote :

The second argument to `post` is the operation:

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

Pass the empty string if you want a plain POST. Try the following:

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

The `apiclient` library is bare-bones; it doesn't help much. We are
working on a better client library. In the meantime, sorry, this is
harder than it should be :-/

Changed in maas:
status: New → Invalid
Binoy M V (binoy-mv) wrote :

Thank you Gavin.

My aim is to send a post to maas. Is there any way to do so ?

Is there any example that we can see, how to pass the data into maas api using post ?

Binoy M V (binoy-mv) on 2015-09-17
Changed in maas:
status: Invalid → In Progress
status: In Progress → Incomplete
Christian Reis (kiko) wrote :

Binoy, can you ask a question using askubuntu.com under MAAS explaining what you are trying to do? This doesn't look like it's a bug in MAAS.

Christian Reis (kiko) on 2015-09-17
Changed in maas:
status: Incomplete → Invalid
Nicola Senno (nsenno) wrote :

for the record here is an example on how to use the POST:

https://gist.github.com/nicolasenno/d39255d130cbde11c8b5868f1be7b310

Blake Rouse (blake-rouse) wrote :

I would recommend using the official python library for MAAS to work with the API.

https://github.com/maas/python-libmaas

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers