[2.0a1] python3-maas-client needs to send data as bytes()

Bug #1555392 reported by David Ames on 2016-03-10
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
MAAS
Critical
Jeffrey C Jones

Bug Description

Testing bleeding edge python3-maas-client API 2.0 with Xenial and python3

The client from both ppa:maas/next and ppa:~maas-maintainers/experimental3 have the problem.

MAASClient put and post (possibly others) do not properly encode request data as bytes() leading to TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str. No matter how I encode the data in the put call this remains true.

In my testing I am trying to update a dnsresource using the 2.0 API.

Traceback (most recent call last):
  File "hooks/test.py", line 81, in <module>
    test_maas()
  File "hooks/test.py", line 75, in test_maas
    dns.update_resource(ip_address)
  File "hooks/test.py", line 41, in update_resource
    ip_address)
  File "/home/thedac/HADNS/device-registration/hooks/maasclient/__init__.py", line 346, in update_dnsresource
    resp = self.driver.update_dnsresource(rid, fqdn, ip_address)
  File "/home/thedac/HADNS/device-registration/hooks/maasclient/apidriver.py", line 243, in update_dnsresource
    ip_address=ip_address)
  File "/home/thedac/HADNS/device-registration/hooks/maasclient/apidriver.py", line 189, in _put
    response = self.client.put(path, **kwargs)
  File "/usr/lib/python3/dist-packages/apiclient/maas_client.py", line 250, in put
    url, method="PUT", headers=headers, data=body)
  File "/usr/lib/python3/dist-packages/apiclient/maas_client.py", line 109, in dispatch_query
    res = urllib.request.urlopen(req)
  File "/usr/lib/python3.5/urllib/request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.5/urllib/request.py", line 463, in open
    req = meth(req)
  File "/usr/lib/python3.5/urllib/request.py", line 1170, in do_request_
    raise TypeError(msg)
TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str.

A quick and dirty fix (likely not the correct one):
--- /usr/lib/python3/dist-packages/apiclient/maas_client.py 2016-03-08 11:16:30.000000000 -0800
+++ maas_client.py 2016-03-09 16:14:12.643817715 -0800
@@ -214,6 +214,7 @@
             body, headers = encode_json_data(params)
         else:
             body, headers = encode_multipart_data(params, {})
+ body = bytes(body, encoding='utf-8')
         self.auth.sign_request(url, headers)
         return url, headers, body

Related branches

Changed in maas:
milestone: none → 2.0.0
importance: Undecided → Critical
summary: - python3-maas-client needs to send data as bytes()
+ [2.0a1] python3-maas-client needs to send data as bytes()
Gavin Panella (allenap) on 2016-03-16
Changed in maas:
status: New → Triaged
Changed in maas:
assignee: nobody → Jeffrey C Jones (trapnine)
Changed in maas:
status: Triaged → In Progress
Changed in maas:
status: In Progress → Fix Committed
Changed in maas:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers