Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

143
Views
Send XMLHttpRequest POST to python cgi

I am sending a text string from javascript to a cgi python3 program. User initiating the send with a button.

With the simplified code I made below the server seems to acknowledge the POST but the cgi does not run. (no output is shown). However if I refresh the page where my button is then the output of the cgi program is now shown on stderr. Strange behaviour.

Any pointers?

I am using a python cgi server.

python3 -m http.server --cgi 8000

the html file:

<html>
<head>
<script>
function post_data(){
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/cgi-bin/cgi.py");
    xhr.setRequestHeader('Content-Type', 'text/html');
    xhr.send("hello world");
    
    xhr.onload = function() {
        console.log(`Loaded: ${xhr.status} ${xhr.response}`);
    };
    xhr.onerror = function() {
        console.log(`Network Error`);
    };
}
</script>
<body>
<button type="button" class="btn btn-primary" onclick="post_data()">Send</button>
</body></html>

The python cgi:

#!/usr/bin/python3
import sys
sys.stderr.write(sys.stdin.read())
print("Content-Type: text/html\n\n")
print("<html><body>ok</body></html>")

The output once the button is clicked:

Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
127.0.0.1 - - [15/Oct/2021 17:03:38] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [15/Oct/2021 17:03:39] "POST /cgi-bin/cgi.py HTTP/1.1" 200 -

after refreshing the page this additional output comes:

hello world127.0.0.1 - - [15/Oct/2021 17:04:03] "GET / HTTP/1.1" 304 -
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!