Max Lopatin Max Lopatin - 1 year ago 81
JSON Question

handle http post request with json by python script on iis

i have trouble handling http post request with JSON as body of the request.
i am running IIS with python as server script.

this is the code that makes request:

var http = new XMLHttpRequest();

var url = "http://myurl.ext/py/";

http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {

data = {"field":"value", "number":5}'POST', url, true);
http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');

on server side i have:

import cgi
import http.client

print("Content-Type: text/text")


cgi.parse() gives empty string

http.client.HTTPResponse gives empty string

cgi.FieldStorage() gives empty string, but if i submit a form, it returns values of the input fields.

i want to send JSON data in the background to the script and return some processed values as JSON as well.

Answer Source

to make it work you have to explicitly tell how much to read.

data = "";

if int(os.environ.get('CONTENT_LENGTH', 0)) != 0:

    for i in range(int(os.environ.get('CONTENT_LENGTH', 0))):
        data +=


this what worked for me