MicroPython urequests.post failures

2019-08-17 04:35发布

I'm trying to receive notifications on my (Android) mobile device from an ESP8266 MCU running MicroPython. For this reason I subscribed to a couple of online services exposing some APIs for this task, Pushbullet, and Pushed, and I installed the matching apps on my device.

This is what I'm trying:

Pushbullet:

import json
import urequests

body = "Test Notification"
title = "Pushbullet"
data_sent = {"type": "note", "title": title, "body": body}
API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx'

pb_headers = {
    'Authorization': 'Bearer ' + API_KEY, 
    'Content-Type': 'application/json'
}

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

print(r)

Error:

ssl_handshake_status: -256
Traceback (most recent call last):
  File "<stdin>", line 11, in <module>
  File "urequests.py", line 104, in post
  File "urequests.py", line 56, in request
OSError: [Errno 5] EIO

Pushed:

import json
import urequests

payload = {
  "app_key": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "app_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
  "target_type": "app",
  "content": "Remote Mic MCU test from ESP8266"
}

r = urequests.post("https://api.pushed.co/1/push", data=payload)
print(r)

Error:

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "urequests.py", line 104, in post
  File "urequests.py", line 74, in request
TypeError: object with buffer protocol required

Searching for these errors, doesn't get me anywhere useful.

The exact same code snippets work OK on my Linux box (using requests instead of urequests), but I understand that urequests may have some limitations.

Do you have any hint on how to fix this?

1条回答
爷的心禁止访问
2楼-- · 2019-08-17 05:17

The exception message suggests that you pass the type of data which urequests doesn't expect. From my knowledge of how HTTP POST works (see HTTP standard), I know that it accepts octet stream, which in Python would be represented by str or bytes type. Whereas you pass a dictionary. `

查看更多
登录 后发表回答