0

I am trying to receive POST requests in Python using the BaseHTTPRequestHandler module. I wrote a simple HTML form:

  <html>
    <head>
      <meta charset='UTF-8'>
    </head>
    <body>
      <form action="http://localhost:8080/" method='post'>
        <strong>Access point</strong><br>
        <label for='ssid'>SSID: </label>
          <input type='text' id=ssid><br>
        <label for='passphrase'>Passphrase: </label>
          <input type='password' id=passphrase><br>
        <br>
        <strong>Calendar</strong><br>
        <label for='id'>ID: </label>
          <input type='text' id=calid><br>
        <br>
        <input type='submit'>
        <input type='reset'>
      </form>
    </body>
  <html>

and an minimal application:

# -*- coding: utf-8 -*-

from http.server import BaseHTTPRequestHandler, HTTPServer
import socketserver
import socket
import sys

class ConfigHTTPRequestHandler(BaseHTTPRequestHandler):
    def _set_headers(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()

    def do_GET(self):
        self._set_headers()
        with open("index.html", "rb") as f:
            self.wfile.write(f.read())

    def do_HEAD(self):
        self._set_headers()

    def do_POST(self):
        print(self.headers)

        content_length = int(self.headers.get('Content-Length', 0))
        config_string = self.rfile.read(content_length).decode("UTF-8")
        print("Content length: ", content_length)
        print("Config string: [ ", config_string, " ]")

        self._set_headers()
        return

ConfigHTTPRequestHandler.protocol_version = "HTTP/1.0"
httpd = HTTPServer(("127.0.0.1", 8080), ConfigHTTPRequestHandler)

sa = httpd.socket.getsockname()
print("Serving HTTP on", sa[0], "port", sa[1], "...")
try:
    httpd.serve_forever()
except KeyboardInterrupt:
    print("\nKeyboard interrupt received, exiting.")
    httpd.server_close()
    sys.exit(0)

The problem is that the every POST request are empty. I get content_length = 0.

2 Answers 2

2

Please provide name attribute to your form elements and see if it works.

<html>
    <head>
        <meta charset='UTF-8'>
    </head>
    <body>
        <form action="http://localhost:8080/" method='post'>
            <strong>Access point</strong><br>
            <label for='ssid'>SSID: </label>
            <input type='text' id="ssid" name="ssid"><br>
            <label for='passphrase'>Passphrase: </label>
            <input type='password' id="passphrase" name="passphrase"><br>
            <br>
            <strong>Calendar</strong><br>
            <label for='id'>ID: </label>
            <input type='text' id="calid" name="calid"><br>
            <br>
            <input type='submit'>
            <input type='reset'>
        </form>
    </body>
<html>
Sign up to request clarification or add additional context in comments.

Comments

0

I would like to elaborate on user7883492's answer.

According to this protocol description only "successful controls" of the form are submitted.
According to this a successful control requires to have a name attribute.

Your controls fulfilled all the requirements to be considered a "successful control", except for the name attribute requirement.
This is the reason they were not submitted.
After adding the name attribute they became "successful controls", and were submitted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.