Socket receives html code instead of plain text

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
cemox
Posts: 34
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

Socket receives html code instead of plain text

Post by cemox » Fri Nov 19, 2021 6:10 pm

Hi, you are all fine.

I wrote a script which first starts in AP mode, asks the user to enter ssid and pasword for the wifi in web form then after restart it starts in station mode and connects to the wifi. Everything works fine, except one thing.

When I enter special characters like "!, ', @", etc. the sockets receives them as HTML code.

For example, when I enter "Cemo" as the "Wifi Name" in the text box and "micro1234!!" for the password, what esp32 receives "micro1234%21&21" as the password. HTML encoding for "!" is %21.

This is true for other characters as well (i.e. "@, ^", etc.)
I know this question is probably not directly related to micropython, but it is quite annoying and I could not find a solution yet.

Code: Select all

b'GET /action_page.php?ssid=Cemo&pwrd=micro1234%21%21 HTTP/1.1\r\nHost: 192.168.4.1\r\n
The server page that runs on esp32 is like this:

Code: Select all

def web_page():
  
    html = """
<!DOCTYPE html>
<head>
<style>
body {
	background-color: lightblue;
}

h2 {
	color: white;
	text-align: center;
}

.clabel {
 	font-family: verdana;
 	font-size: 20px;
}

label {
	width:240px;
    display:inline-block;
}

</style>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">

<html>
<body>

<h2 style="background-color: DodgerBlue;font-family: verdana;">Enter Your Wifi Router Details</h2>

<form enctype="text/plain" action="/action_page.php">
  <label class="clabel" for="ssid">Wifi name (SSID):</label>
  <input type="text" id="ssidID" name="ssid" value="" placeholder="WIFI name"><br><br>
  <label class="clabel" for="pwrd">Password:</label>
  <input type="text" id="pwrdID" name="pwrd" value="" placeholder="Password"><br><br>
  <input class="clabel" type="submit" value="Submit">
</form>

</body>
</html>    
    """
    return html

Does anyone have a solution for this?

User avatar
karfas
Posts: 193
Joined: Sat Jan 16, 2021 12:53 pm
Location: Vienna, Austria

Re: Socket receives html code instead of plain text

Post by karfas » Fri Nov 19, 2021 11:47 pm

Maybe you should read a little about HTTP, HTML and HTML forms.

This is just the way HTTP GET requests with form data are working. They need to encode the reserved characters of an URL (see https://en.wikipedia.org/wiki/Percent-encoding).
You can transfer the required data with a POST request or you can URL-decode the data you get (I'm pretty sure there is a library/package for this).
A few hours of debugging might save you from minutes of reading the documentation! :D
My repositories: https://github.com/karfas

User avatar
cemox
Posts: 34
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

Re: Socket receives html code instead of plain text

Post by cemox » Sat Nov 20, 2021 6:56 am

You are right, thank you.

MasterOfGizmo
Posts: 50
Joined: Sun Nov 29, 2020 8:17 pm

Re: Socket receives html code instead of plain text

Post by MasterOfGizmo » Mon Nov 22, 2021 8:58 pm

Try something like this super simple unquote function:

Code: Select all

    def unquote(self, string):
        bits = string.split('%')

        res = bits[0]
        for item in bits[1:]:
            res = res + chr(int(item[:2], 16)) + item[2:]

        return res

Post Reply