0

I'm using node.js to post a http request. the code works with if i define my post data ahead of the 'options' field, but if I initially set my post_data string to empty and update it later it doesn't pick up the new length. How would I get it to do that ? I'm looking to send multiple posts of varying lengths to the same place in a loop so need to be able to do this.

var post_data=''; //if i set my string content here rather than later on it works

var options = {
        host: '127.0.0.1',
        port: 8529,
        path: '/_api/cursor',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': post_data.length
        }
    };

    var req = http.request(options, function(res) {
        res.setEncoding('utf8');
        res.on('data', function (chunk) {
            console.log('BODY: ' + chunk);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

   post_data = 'a variable length string goes here';//the change in length to post_data is not                     //recognised    
   req.write(post_data);
   req.end();        
user1305541
  • 205
  • 1
  • 4
  • 14
  • Your length is wrong> You need to report the number of bytes as encoded in UTF8; not the number of Unicode codepoints. – SLaks Jun 24 '13 at 19:05
  • please do not use data.length, I bumped to this issue and author said do not use data.length, instead, use Buffer.byteLength(data). Ref question: http://stackoverflow.com/questions/18692580/node-js-post-causes-error-socket-hang-up-code-econnreset and ref issue: https://github.com/visionmedia/express/issues/1749 – Nam Nguyen Sep 09 '13 at 07:40

3 Answers3

1
'Content-Length': post_data.length

You ran this before setting post_data.

If you want to set post_data after creating the object, you'll need to set it manually later:

options.headers['Content-Length'] = post_data.length;

Note that you must set that before calling http.request().

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • mmm... so as I'm using 'req.write' for each post and only ever call 'http.request' once, i can't ever change the size of the data i post – user1305541 Jun 24 '13 at 19:21
  • @user1305541: Of course not. HTTP headers are sent before the payload; you cannot change the header after sending it to the server. – SLaks Jun 25 '13 at 00:23
  • 1
    always use Buffer.byteLength() when you want to find the content length of strings! https://github.com/visionmedia/express/issues/1749 – Nam Nguyen Sep 09 '13 at 07:41
1

You need to replace:

'Content-Length': post_data.length

for:

'Content-Length': Buffer.byteLength(post_data, 'utf-8')

See https://github.com/strongloop/express/issues/1870

Yaiza
  • 11
  • 3
0

Posting data is a matter of sending a query string (just like the way you would send it with an URL after the ?) as the request body.

This also requires to declare Content-Type and Content-Length values so the server knows how to interpret the data.

var querystring = require('querystring');

var data = querystring.stringify({
      username: yourUsernameValue,
      password: yourPasswordValue
    });

var options = {
    host: 'my.url',
    port: 80,
    path: '/login',
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Content-Length': data.length
    }
};

var req = http.request(options, function(res) {
    res.setEncoding('utf8');
    res.on('data', function (chunk) {
        console.log("body: " + chunk);
    });
});

req.write(data);
req.end();
AmirtharajCVijay
  • 1,078
  • 11
  • 12
  • always use Buffer.byteLength() when you want to find the content length of strings! https://github.com/visionmedia/express/issues/1749 – Nam Nguyen Sep 09 '13 at 07:41