MicroPython urequests.post failures

荒凉一梦 提交于 2019-12-11 16:57:59

问题


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:


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. `



来源:https://stackoverflow.com/questions/46939183/micropython-urequests-post-failures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!