Post method Micropython.

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
andermutu99
Posts: 14
Joined: Thu Sep 27, 2018 10:19 am

Post method Micropython.

Post by andermutu99 » Mon Nov 05, 2018 12:26 pm

Dear Community;

I am trying to upload data to Adafruit.io using post method of urequests library. I have tried to options. I both I have ensured I am connected to wifi. The first method is this:

Code: Select all

import urequests as requests
import json

url = "https://io.adafruit.com/api/v2/username/feeds/battery/data?X-AIO-Key=XXXXXXX"

data = {
    "value": "88"
}

r = requests.post(url, data=data)
results = r.json()
print (results)
And I am getting this error:
TypeError: object with buffer protocol required
The second option is the following:

Code: Select all

import urequests as requests
import json
import ujson

url = "https://io.adafruit.com/api/v2/andermutu99/feeds/battery/data?X-AIO-Key=3fe2f3eb5e694346b3f9e709b3128182"

data = {
    "value": "88"
}

data = ujson.dumps(data)
r = requests.post(url, data=data)
results = r.json()
print (results)
With this method I am not getting any error with MicroPython, but I the data is not correctly formatted and Adafruit does not recognize it.
{'error': 'request failed - failed to save data to feed battery. param is missing or the value is empty: datum'}
The first method works perfectly in my PC running in Python 2.7.

Please any help or suggestion is highly appreciated.

Thanks a lot,

Ander.

andermutu99
Posts: 14
Joined: Thu Sep 27, 2018 10:19 am

Re: Post method Micropython.

Post by andermutu99 » Mon Nov 05, 2018 2:28 pm

I have tried a new method that I have seen searching the Web, but it does not work.

Code: Select all

r = requests.post(URL, json =dict(value = '“88“)) 
I get again this error message from Adafruit :
{'error': 'request failed - failed to save data to feed battery. param is missing or the value is empty: datum'}
Please any help is very useful for me.

Thanks in advance.

User avatar
pythoncoder
Posts: 3053
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Post method Micropython.

Post by pythoncoder » Mon Nov 05, 2018 6:04 pm

I get the same error under CPython 3.4.3.

Perhaps this might be relevant?
Peter Hinch

andermutu99
Posts: 14
Joined: Thu Sep 27, 2018 10:19 am

Re: Post method Micropython.

Post by andermutu99 » Tue Nov 06, 2018 9:16 am

pythoncoder wrote:
Mon Nov 05, 2018 6:04 pm
I get the same error under CPython 3.4.3.

Perhaps this might be relevant?
Hi:

Thanks for your answer. I asked for help in a Portuguese MicroPyhton speaking Telegram group and we found a solution:

Code: Select all

import urequests as requests

url = "https://io.adafruit.com/api/v2/username/feeds/battery/data.json"

headers = {'X-AIO-Key': 'xxxxxxxxxxxxxxxxxxx',
           'Content-Type': 'application/json'}

data = '{"value": "50"}'

r = requests.post(url, data=data, headers=headers)
results = r.json()
print(results)
This code works perfectly and now I can upload data to Adafruit.io

Now I have to more topics.

1.-I want to upload a .txt file using urequests module. I have a piece of code that works in Python 2.7, but not in MicroPython. It appears that urequests does not support uploading files.

Code: Select all

files = {'document': ('text.txt', open('/text.txt', 'rb'), 'text/plain', {'Expires': '0'})}
headers = {'Content-Type': 'text/plain'}
r = requests.post(url, files = files, headers=headers)
TypeError: unexpected keyword argument 'files'
There is another method or a solution to upload files?

2.-It would be great if we create an International English speaking MicroPython Telegram group, it is so useful.

Thanks.

Post Reply