Python Forum
Sending data to php page - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Sending data to php page (/thread-25083.html)



Sending data to php page - ebolisa - Mar-18-2020

Hi,

I'm trying to send data to a PHP page, but I get the error

Quote:TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

I appreciate any help on this.

import urllib.request
#import urllib
from urllib.parse import urlencode, quote_plus

apiKeyValue = "xxxx"
board = 2
temp = 12.22
humidity = 33

mydata=[('apiKeyValue','xxxx'),('board','2'), ('temp','22.22'), ('humidity','15')]    #The first is the var name the second is the value
result = urlencode(mydata, quote_via=quote_plus)

path='http://mtpage/post_dht_data.php' 
req=urllib.request.Request(path, result)
#print(req)

req.add_header("Content-type", "application/x-www-form-urlencoded")
page=urllib.request.urlopen(req).read()
print(page)