Micro Python HTTP Write API, Authorization problems and header organization

I am trying to get Micropython to upload data to my Databasebut am running into Authorization errors.

This seem like something that should really work as a lot of people would like to send data from IoT devices running Micro Python to a centeral server.

I am using the urequests library but have not been able to correctl pass the headers to give the database my token, I always get a 401 error.

Currently I am doing this :

‘’’

payload = {“header”:“Authorization: Token CrAzYlOnG_ToKeN”, “data-raw”:“WaterWatcher1 Level=532,Volume=752.22,BattV=3.768,Temperature=25.36 1621434110”}

response = urequests.post(“http://xxx.xxx.xxx.xxx:8086/api/v2/write?org=Home_Org&bucket=BackYardInfo&precision=s”, params=payload)


But I always get back this :

W (3390149) HTTP_CLIENT: This request requires authentication, but does not provide header information for that

As the database gives back a response I am sure most of it is working, the problem being the way I am passing the Auth variables.

I have tried using curl but that has the same problems and I would like to use Micropython.

I would like to know if anyone has had this problem or has been able to pass data with Micropython before ?

Or alternativly if I can just create one really long URL to send everything at once ?

New to most of this so any pointers would be a great help.

hi @Duck999 ,

i am not using it exactly as you, but will show you my way

  • on my ESP32 i am not using wifi. from micropython i send it via lora and then have my lora_payload_handler and push it in iflux2 via curl

if micropython urequest is not working, be sure that you can succeed with curl for same data. this way you know your data are valid (very often you can have mistake as missing organization or wrong timestamp format or …) [your timestamp is len() 10 so i believe it is in SEC]

let’ say:

$curl --request POST “https://ruth:8086/api/v2/write?org=foookin_paavel&bucket=hemichromis” --header “Authorization: Token …” --data-raw “scale,host=spongebob,ScaleSenKey=1,ScaleUlId=7c9ebdf1ff64 ScaleDecimal=11.773,ScaleKey=15937 1621768776611”

-this will not input any data as my timestamp is MS format (but i did not specify that)

$curl --cacert /home/conan/.ssh/ruth/influxdb-selfsigned.crt --request POST “https://ruth:8086/api/v2/write?org=foookin_paavel&bucket=hemichromis&precision=ms” --header “Authorization: Token …” --data-raw “scale,host=spongebob,ScaleSenKey=1,ScaleUlId=7c9ebdf1ff64 ScaleDecimal=11.773,ScaleKey=15937 1621768776611”

  • here it will succeed as PRECISION is used with correct MS [not default influx2 NS]

hope that helps, pavel

Hi, thanks for the input, I actually managed to figure it out late last night, here is what worked for me.

Basically I used the mrequests lib ( https://github.com/SpotlightKid/micropython-stm-lib/tree/master/mrequests )

With this I managed to get the whole thing working as expected using the following code - Done in Micropython

I did make a free account with No-IP to set my ISP external IP to a URL, I dont think this has a massive change but it is worth noting. I did try the older methods with this new URL and they still did not work.

Here is the new code with the mrequest lib.

import mrequests
url = "http://ni-ip-address:8086/api/v2/write?org=Home_org&bucket=backYardData&precision=s"

token = "Token CrazyLongToken"


DataString = "WaterWatcher1 Level=888,Volume=888.88,BattV=8.888,Temperature=25.36 1621694039"

res = mrequests.request("POST", url, headers={"Authorization":token}, data=DataString)

This works perfectly for me.

On a side note, I can also upload multiple points if I read them from a file but I had to put the newline char “\n” at the front of each of my lines not the end or Influx would not pars the data.

I think it boiled down to the urequest lib not being able to do headers as I needed them in post requests. I may have just been using it wrong but I feel that I did give it more than a fair go at getting it right where as this mrequests lib just worked perfectly.

Hope this helps anyone else out there one day.
Thanks again for your input srbp :+1:

nice, i have just tested in python3.7 as i was curious

import requests

url
https://ruth:8086/api/v2/write?org=foookin_paavel&bucket=ccc&precision=ms
token
‘…’
data
‘battery_adc,host=spongebob,BatSenKey=17,BatUlId=50x29196a858,BatCarrier=ttn,BatValid=true BatDecimal=6.87,BatKey=19651 1621770008406’

r = requests.post(url, verify=“/full_path_2_my_cert”, headers={‘Authorization’: ‘Token {}’.format(token)}, data=data)

have fun with it, pavel

r
<Response [204]>