Unable to POST data
Posted: Sun Aug 09, 2020 9:09 pm
Hi,
I'm trying to post data using an ESP32. A similar code shown below works fine when executed from a Raspberry.
I'm getting a response from the server stating that the api key is wrong, but it's not so, I'm sure I've an error in my code.
I appreciate some feedback.
TIA
EDIT:
Solved. My issue was related to the data sent. It should by in String format not dictionary.
Display console:
I'm trying to post data using an ESP32. A similar code shown below works fine when executed from a Raspberry.
I'm getting a response from the server stating that the api key is wrong, but it's not so, I'm sure I've an error in my code.
I appreciate some feedback.
TIA
EDIT:
Solved. My issue was related to the data sent. It should by in String format not dictionary.
Code: Select all
import urequests, ujson, utime
# API key here
API_KEY = '0000'
# defining the url
url = "https://www.mydomain.com/ip_post.php"
myip=wlan.ifconfig()[0]
# data to be sent to api
mydata = {'api_key':API_KEY,
'board':'2',
'temp':'23',
'uptime':'33',
'ip': myip
}
def main():
# sending post request and saving response as response object
header = {'Content-type': 'application/x-www-form-urlencoded; charset=utf-8'}
#header = headers = {'Content-Type': 'multipart/form-data;boundary="boundary"'}
response=urequests.post(url, data=mydata, headers=header)
#response = urequests.post(url, data = mydata)
# extracting response text
url_response = response.text
print("The response from URL is:%s"%url_response)
print(mydata)
if __name__ == '__main__':
try:
#update()
print('--- Booted and time set ---')
while True:
main()
gc.collect() #clear memory and display
utime.sleep(2)
except SyntaxError as e:
print(e)
MPY: soft reboot
my ip: 192.168.0.12
NTP server query successful.
{'api_key': '0000', 'temp': '23', 'uptime': '33', 'ip': '192.168.0.12', 'board': '2'}
--- Booted and time set ---
The response from URL is: Wrong API Key provided.
The response from URL is: Wrong API Key provided.
The response from URL is: Wrong API Key provided.
The response from URL is: Wrong API Key provided.
The response from URL is: Wrong API Key provided.