Python Forum

Full Version: Sending data to php page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)