Flask doesn't see JSON data sent by Node
<p>I am trying to send JSON data to Flask using Node, but I can't read the data in Flask.  I tried printing <code>request.data</code> in Flask but it didn't output anything.  I also tried printing <code>request.json</code>, but it returned a 400 response.  Why doesn't Flask see the JSON data sent by Node?</p>

<pre><code>from flask import Flask
from flask import request

app = Flask(__name__)

@app.route("/", methods=['GET', 'POST'])
def hello():
    if request.method == "POST":
        print "POST";
        print "get_json: ", request.get_json();
        # print "get_json: ", request.get_json(force = True);
        print "data: ", request.data;
        return 'POST';
    else:
        print "GET";
        return "GET";

if __name__ == "__main__":
    app.run()
</code></pre>

<pre class="lang-javascript prettyprint-override"><code>var async = require('async'),
    http = require('http'),
    util = require('util');

var server = {
        hostname: '127.0.0.1',
        port: 5000
    };

function request(method, path, headers, body, callback) {
    var req = {};

    if(!headers) {
        headers = {};
    }

    headers['Content-Type'] = 'application/json';

    console.log(method + ' ' + path);
    console.log(' Req:');
    console.log('  headers: ' + JSON.stringify(headers));
    if(body) {
        console.log('  body   : ' + JSON.stringify(body));
    } else {
        console.log('  no body');
    }

    req = http.request({
            hostname: server.hostname,
            port: server.port,
            path: path,
            method: method,
            headers: headers
        }, (res) =&gt; {
            var resbody = '';

            res.on('data', (chunk) =&gt; {
                resbody = resbody + chunk;
            });

            res.on('end', () =&gt; {
                console.log(' Res:');
                console.log('  headers: ' + JSON.stringify(res.headers));
                if(body) {
                    console.log('  body   : ' + JSON.stringify(resbody));
                } else {
                    console.log('  no body');
                }               
                callback(resbody);
            });
        }
    );

    req.on('error', (err) =&gt; {
        console.log(method + ' ' + path + ' ERR: ' + util.inspect(err));
        callback(err);
    });

    if(body) {
        req.write(JSON.stringify(body));
    }

    req.end();
}

request('POST', '/', null, {foo: 'bar'}, (res) =&gt; {});
</code></pre>

<hr>

<p>Output from JavaScript:</p>

<pre class="lang-none prettyprint-override"><code>POST /
 Req:
  headers: {"Content-Type":"application/json"}
  body   : {"foo":"bar"}
 Res:
  headers: {"content-type":"text/html","content-length":"192","server":"Werkzeug/0.11.11 Python/2.7.11","date":"Fri, 09 Sep 2016 10:29:58 GMT"}
  body   : "&lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\"&gt;\n&lt;title&gt;400 Bad Request&lt;/title&gt;\n&lt;h1&gt;Bad Request&lt;/h1&gt;\n&lt;p&gt;The browser (or proxy) sent a request that this server could not understand.&lt;/p&gt;\n"
</code></pre>

<p>Output from Python:</p>

<pre class="lang-none prettyprint-override"><code>POST
get_json: 127.0.0.1 - - [09/Sep/2016 12:29:58] "POST / HTTP/1.1" 400 -
</code></pre>

<hr>

<p><em>Edited to update all codes</em>.</p>

<p>curl:</p>

<pre class="lang-none prettyprint-override"><code>&gt; curl.exe 127.0.0.1:5000 --header "Content-Type: application/json" --data {\"foo\":\"bar\"}
POST
</code></pre>

<p>The Python Server is fine, and run correctly, the problem lies in the handcrafted http request, which for some reason is malformed.</p>

<p>Using the <a href="https://github.com/request/request" rel="nofollow"><code>request</code></a> module works:</p>

<pre><code>var request = require('request');

request({
    method: 'POST',
    url: 'http://127.0.0.1:5000',
    // body: '{"foo": "bar"}'
    json: {"foo": "bar"}
}, (error, response, body) =&gt; {
    console.log(error);
    // console.log(response);
    console.log(body);
});
</code></pre>

javascript python json node.js flask