Cam.Davidson.Pilon
Cam.Davidson.Pilon

Reputation: 1726

POST request to Heroku results in Python IncompleteRead Error

My Python script, with lines:

from requests import post

...
while(1):

   result = readSensors().result

   payload["z"] = (result['zforce'])
   payload["x"] = (result['xforce'])
   payload["y"] = (result['yforce'])
   payload["light"] = (result['light'] )
   payload["pitch"] = ( result["pitch"] )
   payload["azimuth"] = ( result["azimuth"] )
   payload["roll"] = ( result["roll"] )

   post(SERVER, data = payload )
   sleep(0.02)

to my Heroku webapp server results in a Python error httplib.IncompleteRead: IncompleteRead(0 bytes read) error.


The web.js file looks like:

var server = http.createServer(function(request, response){     
  if (request.method == 'POST'){
            var body = '';
            request.on('data', function (data) {
                body += data;
            });
            request.on('end', function () {
                //send data to clients.
                io.sockets.emit( 'data', parse(body) );

            });
    response.end()

}
server.listen(process.env.PORT || 5000);


var io = io.listen(server);

io.set('log level', 1);
io.configure(function () { 
  io.set("transports", ["xhr-polling"]); 
  io.set("polling duration", 10);  
});

Upvotes: 1

Views: 1348

Answers (1)

Aya
Aya

Reputation: 42030

This is more likely to be an issue with your "webapp server" rather than your client code, so you'll have to include more info about the server.

One common reason for this is that you're sending back a response with Transfer-Encoding: chunked, but not providing a response body which is chunk-encoded correctly.

For example, the following server code...

from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyRequestHander(BaseHTTPRequestHandler):

    def do_GET(self):
        self.send_bogus_response()

    def do_POST(self):
        self.send_bogus_response()

    def send_bogus_response(self):
        self.send_response(200)
        self.send_header('Content-Type', 'text/plain')
        self.send_header('Connection', 'close')
        self.send_header('Transfer-Encoding', 'chunked')
        self.end_headers()

server = HTTPServer(('', 8000), MyRequestHander)
server.serve_forever()

...will cause the same error when called with the requests library...

>>> import requests
>>> requests.post('http://localhost:8000', data={'foo':'bar'})
...
httplib.IncompleteRead: IncompleteRead(0 bytes read)

Apparently, this problem can also happen if the server doesn't read the entire request body, and there's probably a few other reasons as to why this could be happening, but I can't be sure which is most likely without knowing more about the server setup.


Update

There's quite a few issues with the web.js code, but the main problem is that you're sending back a response before having read the POST data. The response.end() needs to go inside the request.on('end', ...) function, otherwise neither of your request.on(...) functions will ever get called.

The following code should eliminate that problem...

var server = http.createServer(function(request, response)
{
    if (request.method == 'POST')
    {
        var body = '';

        request.on('data', function(data)
        {
            body += data;
        });

        request.on('end', function()
        {
            //send data to clients.
            io.sockets.emit('data', parse(body));
            response.end();
        });
    }
});

...although I'm not sure what the parse(...) function is supposed to be.

Upvotes: 1

Related Questions