maas 2.2 api returning malformed data

Bug #1730783 reported by rory schramm
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MAAS
Invalid
Undecided
Unassigned

Bug Description

Hi I'm using python3 to query maas by api and most of them time I'm getting valid http/json data back from lib request. However, sometimes the data that I get back from the maas api is malformed.

here is the python script I'm using:

```
from requests_oauthlib import OAuth1 as oauth
import requests
import json

client_key = 'my_client_key'
client_secret = ''
res_key = 'my_resource_key'
res_secret = 'my_resource_secret'

def myq(url='http://127.0.0.1:5240/MAAS',uri='None'):
        headeroauth = oauth(client_key, client_secret, res_key,
        res_secret, signature_type = 'auth_header')
        r = requests.get(queryurl, auth = headeroauth)
        return r

r = myq(uri='/api/2.0/machines/ww4467/')
try:
    machine = yaml.load(r.text)
    m = {
            'hostname': machine['hostname']
        }
    print('good:', r,dir(r))

except Exception:
    print('bad:', r, dir(r))
    print(r.text)
```

also the librequests json object from the returned query throws the following exception if I try to use that as well when I get a malformed response.

ie

```
r = myq(uri='/api/2.0/machines/ww4467/')
machine = r.json()

Traceback (most recent call last):
  File "./pyapi.py", line 51, in <module>
    machine = r.json()
  File "/home/sysadmin/maas/lib/python3.5/site-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python3.5/json/__init__.py", line 319, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.5/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
```

I don't see any errors in any of the mass log files or syslog.

Revision history for this message
rory schramm (etfeet) wrote :
Revision history for this message
Mike Pontillo (mpontillo) wrote :

I think this is a duplicate of an existing bug. (I'll link it if I find it.) You probably need to set an 'Accept' header to get consistent results. Long story short: MAAS makes use of a Python library that has some undefined behavior when you don't define one in your request, such as:

    Accept: application/json

Changed in maas:
status: New → Invalid
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.