urequests.post failures
urequests.post failures
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.
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.
Re: urequests.post failures
Anyway, why BBCode is OFF?
Re: urequests.post failures
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
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
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: urequests.post failures
Code: Select all
r = urequests.post("https://api.pushed.co/1/push", data=payload)
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'))
https://github.com/micropython/micropyt ... sts.py#L35
Re: urequests.post failures
Unfortunately, still no joy (unless I'm missing something).torwag wrote: ↑Thu Oct 26, 2017 10:10 pmFirst 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
Thanks for the reply. I'll take a look into this.SpotlightKid wrote: ↑Sun Oct 29, 2017 2:07 pmdata needs to be of type bytes or bytearray, not a dictionary.Code: Select all
r = urequests.post("https://api.pushed.co/1/push", data=payload)
If you want to send JSON encoded data, use the json keyword arg, passing a dictionary to it:
See the source here:Code: Select all
r = urequests.post(url, json=dict(foo='bar'))
https://github.com/micropython/micropyt ... sts.py#L35
For the moment, I have the "pushbullet" part working (https://github.com/micropython/micropython/issues/3389).
Re: urequests.post failures
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."}}
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: urequests.post failures
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.
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.
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.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: urequests.post failures
Here's an example, where I did just that. You still need the urequests and collections.defaultdict module from micropython-lib.SpotlightKid wrote: ↑Fri Nov 03, 2017 6:42 pmYou might want to just copy and paste the quote, quote_plus and urlencode functions from it into your code.
https://gist.github.com/SpotlightKid/ec ... 635f62ab73
Re: urequests.post failures
Hello SpotlightKid,SpotlightKid wrote: ↑Sat Nov 04, 2017 5:07 pmHere's an example, where I did just that. You still need the urequests and collections.defaultdict module from micropython-lib.SpotlightKid wrote: ↑Fri Nov 03, 2017 6:42 pmYou might want to just copy and paste the quote, quote_plus and urlencode functions from it into your code.
https://gist.github.com/SpotlightKid/ec ... 635f62ab73
thank you very much for your answer.
I'll try to implement it and I'll let you know.