Error in micro-python doing a POST using "urequests"

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
jarain78
Posts: 1
Joined: Tue Sep 11, 2018 11:05 am

Error in micro-python doing a POST using "urequests"

Post by jarain78 » Tue Sep 11, 2018 11:10 am

Hello everybody, I have a problem, I'm trying to send a wav file using the "urequests" library, but I can't do it. The micro-piton returns the following error:

TypeError: object with buffer protocol required

Any one knows how I can solve this?.

This is my code:

url = "http://server_addres/wav_file"
fin = wave.open('/sd/temp.wav', 'rb')

file_to_send= {'file': ubinascii.b2a_base64(fin)}
header={"content-type":"audio/wav", 'file': ubinascii.b2a_base64(fin)}

# data=file_to_send,

try:
r = urequests.post(url, headers=header)
print (r.text)
finally:
print("Close...")
fin.close()

Regards

Jarain78

SpotlightKid
Posts: 377
Joined: Wed Apr 08, 2015 5:19 am

Re: Error in micro-python doing a POST using "urequests"

Post by SpotlightKid » Tue Sep 11, 2018 12:25 pm

Your code contains several errors:

* You're trying to encode the file-object fin via binascii.b2sa_base64. You probably meant to encode the filename.
* It's the line with the assignment to file_to_send which therefor fails.
* The same error is in the next line, but, anyways, the filename does not belong into the "Content-Type" header. I believe, if you want to pass a filename, you have to use a multipart body and set a "Content-Disposition" header on the part containing the file.
* You need to pass the the request body with the data argument.
* I don't see the need to use Base64 encoding at all.

Code: Select all

data = open("foo.wav", 'rb').read()
headers = {'Content-type': "audio/wav"}
r = urequests.post(url, data=data, headers=headers)

Post Reply