ESP WROOM 32 urequests POST to Google Forms

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
eGRan
Posts: 7
Joined: Wed May 29, 2019 4:20 pm

[SOLVED] ESP WROOM 32 urequests POST to Google Forms

Post by eGRan » Mon Jun 03, 2019 2:08 am

In order to post to a Google Form directly the following changes need to be made:

Code: Select all

import urequests

# The URL is the copied link that is shared with others. Formatting is string
url = 'https://docs.google.com/forms/d/e/<form_id>/formResponse'

# The following header MUST be included:
headers = {'Content-Type': 'application/x-www-form-urlencoded'}

# Form data seems to follow a urlencoded format. It is a string with entries=values separated by '&'
form_data = 'entry.entry_id_1=value_1&entry.entry_id_2=value_2' 

# send the url, form_data and headers using urequest.post()
r = urequests.post(url, data=form_data, headers=headers)

#Close the connection to prevent memory errors
r.close()
Thanks again to jimmo who figured this out. Much appreciated!

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

Re: ESP WROOM 32 urequests POST to Google Forms

Post by SpotlightKid » Tue Jun 04, 2019 1:32 pm

I wrote a Github gist on the topic of making POST request with form-encoded params with urequest a while ago:

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

The "urllib.parse" module in miceropython-lib, which contains the "urlencode" function is rather large and depends on other large modules, so I extracted the "urlencode" function into a smaller stand-alone module, so you can use it even on an ESP8266 unit.


Chris

Post Reply