urequests object with buffer protocol required

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
johncblacker
Posts: 9
Joined: Sun Nov 11, 2018 8:11 pm

urequests object with buffer protocol required

Post by johncblacker » Tue Mar 30, 2021 4:14 pm

I'm trying to get a program to push a notification to my phone via pushbullet. I found some code that supposedly works(or worked at some time) and get an error:
File "C:\Users\jobla\Documents\Micropython-Projects\workSpace\RNT-ESP32-ESP8266\visitorpush.py", line 87, in <module>
File "C:\Users\jobla\Documents\Micropython-Projects\workSpace\RNT-ESP32-ESP8266\visitorpush.py", line 63, in visitornotify
File "/lib/urequests.py", line 115, in post
File "/lib/urequests.py", line 68, in request

TypeError: object with buffer protocol required

The relevant request is:

def visitornotify():
r = urequests.post(
'https://api.pushbullet.com/v2/pushes',
#data=json.dumps(data_sent),
#'Host':'api.pushbullet.com',
data=data_sent,
headers=pb_headers)

Line 53 is the 'https....' line.

data_sent = {"type": "note", "title": title, "body": body}

Don't know how to fix this...anyone have some wisdom to share on this?
Thanks.

marcidy
Posts: 133
Joined: Sat Dec 12, 2020 11:07 pm

Re: urequests object with buffer protocol required

Post by marcidy » Tue Mar 30, 2021 6:00 pm

try:

Code: Select all

urequest.post(url, json=data_send)
json.dumps will be called on the json parameter by urequests.

otherwise data takes bytes, not a string, so you need to encode the data

Code: Select all

urequests.post(url, data=json.dumps(data_send).encode('utf-8'))

Post Reply