http POST with chunked body - not supported?

Posted on
  • epsruino board 1v4, fw 1v80, cc3000, http client

    i expted the code below to send 3 chunks (snippet#1):

    function post_test1(){       
    var o = {
    host: BACKEND_IP,
    port: BACKEND_PORT,
    path: BACKEND_ROOT, 
    method:'POST',
    headers: {
      "Content-Type" : "application/json",
      "Transfer-Encoding": 'chunked'
    }
    };
    var req= http.request( o, function(res)  {    
    res.on('close', function() { 
      console.log( 'post closed',res.statusCode);  
    });       
    res.on('end', function() { 
      console.log( 'post end',res.statusCode);  
    });       
    });
    
    setTimeout( function(){
      var c = 'abcd';
      console.log('sending',c);
      req.write( c);         
    }, 1000);
    
    setTimeout( function(){
      var c = 'zyxw';  
      console.log('sending',c);
      req.write( c);         
    }, 3000);
    
    setTimeout( function(){
      c='the end';
      console.log('sending',c);
      req.end( c);         
    }, 5000);
    }
    

    but there is nothing (not even a header) received by the server. i detected two major issues.

    A) the req object does mostly not work in the setTimeout callback - reqseems to be valid until next idle state only (or a few milliseconds longer 'cause the bad code below worked in very seldom trials)

    // this is fine - snippet#2a
      req.write( 'abcd');         
      req.end( 'the end');         
    
    // this is - mostly - bad - snippet#2b
    req.write( 'abcd');         
    setTimeout( function(){
      req.end( 'the end');         
    }, 1000);
    

    B) req.write(...) does not produce a chunked payload / does not encode chunks;

    // this should send two chunks: - snippet#3a
      req.write( 'abcd');         
      req.end( 'the end');         
    

    at least it sends the data, but does no chunk encoding

    // the chunk encoding has to be added manually:- snippet#3b
      req.write( '4\r\nabcd\r\n');         
      req.end( '7\r\nthe end\r\n0\r\n\r\n');
    

    the code snippet#1 runs from node.js 0.12.4 as expected. seems that the espruino http client does some things different from node.js' http client (which applies chunked encoding by default - as long as you to not set the content-length header).

    is any workaround / solution possible?

    thx!

  • Hi, I'll take a look at this now and will see if I can get a fix in for it - presumably it hasn't been tested with a payload and a delay.

    In terms of the chunked encoding, I wasn't aware that was even a requirement. I'll see what can be done there - it's literally data.length+"\r\n"+data+"\r\n"?

  • Ok, fixed. If you check for the build of 5f994d076b51123567560989f603be01577adbf3 in about an hour there should be some binaries in there.

    If you paste the URL of the correct one for your device (probably espruino_1v80.XXX_espruino_1r3.bin) into the Web IDE's advanced flasher and click 'go', you should get the new build with that fixed.

  • it's literally data.length+"\r\n"+data+"\r\n"?

    be aware of base 16 encoding of data.length - e.g. data.length.toString(16)

    ps: respect - i am absolutely surprised about your speedy response - i'll check out your update 'till tomorrow morning (CET ;)

  • Ahh, thanks! I should have checked the spec properly.

    So you're now after build 1e90b57df64ffe9108e94383dcc47a5ca2b0a8d9 instead :)

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

http POST with chunked body - not supported?

Posted by Avatar for andiy @andiy

Actions