urequests.post failures

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
dentex
Posts: 9
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

urequests.post failures

Post by dentex » Wed Oct 25, 2017 6:12 pm

Hello!
This is my 1st post here; I hope I'm not breaking any rules.

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:

[code]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)[/code]

Error:

[code]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[/code]

Pushed:

[code]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)[/code]

Error:

[code]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[/code]

Searching for those 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? Thanks.
Last edited by dentex on Tue Oct 31, 2017 8:56 pm, edited 1 time in total.

dentex
Posts: 9
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

Re: urequests.post failures

Post by dentex » Thu Oct 26, 2017 7:04 am

Anyway, why BBCode is OFF? :?

torwag
Posts: 219
Joined: Fri Dec 13, 2013 9:25 am

Re: urequests.post failures

Post by torwag » Thu Oct 26, 2017 10:10 pm

First posts do not allow bbcode as some spammers used it to send pictures not only of naked PCBs but of other naked things....
As we need to approve the first posts and they always open up full for approval, we had an office-rules violation from time to time ;)
Now it should work for you

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

Re: urequests.post failures

Post by SpotlightKid » Sun Oct 29, 2017 2:07 pm

Code: Select all

r = urequests.post("https://api.pushed.co/1/push", data=payload)
data needs to be of type bytes or bytearray, not a dictionary.

If you want to send JSON encoded data, use the json keyword arg, passing a dictionary to it:

Code: Select all

r = urequests.post(url, json=dict(foo='bar'))
See the source here:

https://github.com/micropython/micropyt ... sts.py#L35

dentex
Posts: 9
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

Re: urequests.post failures

Post by dentex » Tue Oct 31, 2017 8:59 pm

torwag wrote:
Thu Oct 26, 2017 10:10 pm
First posts do not allow bbcode as some spammers used it to send pictures not only of naked PCBs but of other naked things....
As we need to approve the first posts and they always open up full for approval, we had an office-rules violation from time to time ;)
Now it should work for you
Unfortunately, still no joy (unless I'm missing something).
SpotlightKid wrote:
Sun Oct 29, 2017 2:07 pm

Code: Select all

r = urequests.post("https://api.pushed.co/1/push", data=payload)
data needs to be of type bytes or bytearray, not a dictionary.

If you want to send JSON encoded data, use the json keyword arg, passing a dictionary to it:

Code: Select all

r = urequests.post(url, json=dict(foo='bar'))
See the source here:

https://github.com/micropython/micropyt ... sts.py#L35
Thanks for the reply. I'll take a look into this.
For the moment, I have the "pushbullet" part working (https://github.com/micropython/micropython/issues/3389).

dentex
Posts: 9
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

Re: urequests.post failures

Post by dentex » Fri Nov 03, 2017 9:37 am

So I tried again with Pushed: using the json keyword as suggested, the request goes, but the response text is:

Code: Select all

{"error":{"type":"empty_credentials","message":"You must introduce account credentials to make requests. Please read the documentation available in https:\/\/about.pushed.co\/docs."}}

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

Re: urequests.post failures

Post by SpotlightKid » Fri Nov 03, 2017 6:42 pm

This API endpoint form pushed.co actually expects form-encoded values not JSON:

https://about.pushed.co/docs/api#api-method-push

In CPython's urllib.request.urlopen and requests.post you can just pass a dictionary to the data keyword parameter and the keys and values will be send as the form-encoded body and appropriate headers will be set. Not so in urequests. There you have to do the form-encoding yourself. If you're not short on memory, you can install the urllib.parse module from micropython-lib and use it like this.

Code: Select all

import urequests
from urllib.parse import urlencode
url = 'http://httpbin.org/post'
data = dict(foo='bar', spamm=42)
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
response = urequests.post(url, data=urlencode(data), headers=headers)
print(response.json())

But it's a very heavy module and contains many things that you might not need. You might want to just copy and paste the quote, quote_plus and urlencode functions from it into your code.

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

Re: urequests.post failures

Post by SpotlightKid » Sat Nov 04, 2017 5:07 pm

SpotlightKid wrote:
Fri Nov 03, 2017 6:42 pm
You might want to just copy and paste the quote, quote_plus and urlencode functions from it into your code.
Here's an example, where I did just that. You still need the urequests and collections.defaultdict module from micropython-lib.

https://gist.github.com/SpotlightKid/ec ... 635f62ab73

dentex
Posts: 9
Joined: Wed Oct 25, 2017 4:49 pm
Contact:

Re: urequests.post failures

Post by dentex » Mon Nov 06, 2017 6:57 pm

SpotlightKid wrote:
Sat Nov 04, 2017 5:07 pm
SpotlightKid wrote:
Fri Nov 03, 2017 6:42 pm
You might want to just copy and paste the quote, quote_plus and urlencode functions from it into your code.
Here's an example, where I did just that. You still need the urequests and collections.defaultdict module from micropython-lib.

https://gist.github.com/SpotlightKid/ec ... 635f62ab73
Hello SpotlightKid,
thank you very much for your answer.
I'll try to implement it and I'll let you know. :)

Post Reply