adafruit / Adafruit_CircuitPython_Requests

Requests-like interface for web interfacing
MIT License
51 stars 37 forks source link

TypeError: wrong number of arguments when posting bytes array #20

Closed jimbobbennett closed 4 years ago

jimbobbennett commented 4 years ago

I have a bytearray containing image data from the TTL camera sample. I want to POST this as an octet stream to a REST endpoint.

My code:

import adafruit_requests as requests

...

buffer = bytearray()

headers={
            'Content-Type': 'application/octet-stream',
            'Prediction-Key': prediction_key
        }

r = requests.request('POST', prediction_endpoint, data=buffer, headers=headers)

When I run this I get the following:

......Traceback (most recent call last):
  File "code.py", line 85, in <module>
  File "adafruit_requests.py", line 242, in request
  File "adafruit_requests.py", line 224, in request
TypeError: wrong number of arguments

Running the code locally I've tracked down the error to this line:

if data:
   sock.send(bytes(data, "utf-8"))

If I change the call to bytes() to remove the encoding parameter, it works.

if data:
   sock.send(bytes(data))

Does the CircuitPython implementation of bytes have an encoding parameter?

jimbobbennett commented 4 years ago

PR #21 raised to remove this parameter, if this is the correct fix

FoamyGuy commented 4 years ago

Odd, I am able to run this at REPL and don't get the same error.

>>> bytes("hello", "utf-8")
b'hello'
jimbobbennett commented 4 years ago

@FoamyGuy - I think it is because it is a bytearray.

Just tested in the REPL:

>>> b = bytearray(10)
>>> bytes(b, 'utf-8')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: wrong number of arguments 
FoamyGuy commented 4 years ago

Looks like possible the issue is more related to the bytearray than the encoding param I use a bytebuffer instead of a string it reports the error:

>>> buffer = bytearray()
>>> bytes(buffer, "utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: wrong number of arguments
FoamyGuy commented 4 years ago

This does not appear to be possible in CPython 3.7 either, though the error is different than CircuitPython:

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> buffer = bytearray()
>>> bytes(buffer, "utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: encoding without a string argument
jimbobbennett commented 4 years ago

I've changed my PR to check the type, and not pass the encoding for bytearray, but keep it for other types.

jimbobbennett commented 4 years ago

Just tested this with the latest bundle and it's all working. Thanks @FoamyGuy and @tannewt for your help with this.