handle http post request with json by python script on iis
<p>i have trouble handling http post request with JSON as body of the request. 
i am running IIS with python as server script.</p>

<p>this is the code that makes request: </p>

<pre><code>var http = new XMLHttpRequest();

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

http.onreadystatechange = function() {
if(http.readyState == 4 &amp;&amp; http.status == 200) {
    console.log(http.responseText);
    }
}

data = {"field":"value", "number":5}

http.open('POST', url, true); 
http.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
http.send(JSON.stringify(data));
</code></pre>

<p>on server side i have: </p>

<pre><code>import cgi
import http.client

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

print(cgi.parse())
print(http.client.HTTPResponse)
</code></pre>

<p>cgi.parse() gives empty string</p>

<p>http.client.HTTPResponse gives empty string</p>

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

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

<p>to make it work you have to explicitly tell how much to read.
<br/></p>

<pre><code>data = "";

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

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

print(data)
</code></pre>

<p>this what worked for me</p>

python json iis post request