
Timezone: Europe/London
The above PHP script, if requested from a browser (which uses a GET request), would show two text boxes for first and last name, and a submit button.
Completing the form, and clicking Submit, POSTs the information in the boxes to the PHP script, which then acts on the data.
The urequests module in Micropython can also sent POST requests, though it is not quite so straight forward as using GET. When I was experimenting on how to do this I drew a blank at first
It seemed simple at the outset. The general advice on multiple websites was to send the data in key pairs, either as data or as a json.
Eg. Combinations such as:
data = {key1: value, key2: value2}
response = requests.post(request_url, data=data)
or use response = requests.post(request_url, json=data)
alternatively, use data_encoded = json.dump(data) and one of the above lines.
None of them worked, even if I tried different combinations, with either
headers = {'content-type': 'application/x-www-form-urlencoded'}
or headers = {'content-type': 'application/json'}
I finally found the answer though. The above will work in some form with Python, but MicroPython is a subset of Python, and to restrict it's size, hence the name, there are lots of Python that has not been included. Python Responses does a lot more behind the scenes encoding which uResponses does not.
In uPython, you have to encode the data as a string, and use the content header “headers = {'content-type': 'application/x-www-form-urlencoded'}”
For the string, just precede the key names with “&” and use an equals sign for the values.
data = {key1: value, key2: value2} becomes data = ‘&key1=value&key2=value2’
Very much like a GET query string without the first value being preceded by a question mark.
Eg. GET url/script.php?key1=value&key2=value2
So, if you call the PHP script shown above from urequests with a post request using this code, the response.text should read "Welcome John Smith. You have made a POST request."
IDE editors for programming ESP boards.
Micropython editors
Micropython functionality and libraries.
Micropython.org
Find the uPython firmware for your board.
uPython firmware for development boards.
ESP32 uPython modules and function examples.
Quick reference for the ESP32
Scanning and connecting to multiple SSIDs
Using an SD card reader breakout board
OTA Updating running software without interuption
Hassle free web communication with urequests.py.
Future articles
Communication between ESPs