Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
doc: http.request() improved code example
Browse files Browse the repository at this point in the history
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
Reviewed-by: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
guisouza authored and trevnorris committed Sep 17, 2014
1 parent 7ca5af8 commit 468fb54
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions doc/api/http.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,19 @@ upload a file with a POST request, then write to the `ClientRequest` object.

Example:

var postData = querystring.stringify({
'msg' : 'Hello World!'
});

var options = {
hostname: 'www.google.com',
port: 80,
path: '/upload',
method: 'POST'
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};

var req = http.request(options, function(res) {
Expand All @@ -510,8 +518,7 @@ Example:
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.write(postData);
req.end();

Note that in the example `req.end()` was called. With `http.request()` one
Expand Down

2 comments on commit 468fb54

@mscdex
Copy link

@mscdex mscdex commented on 468fb54 Sep 17, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Buffer.byteLength(postData) should be used instead of postData.length for the Content-Length in case someone modifies the example and uses strings with multi-byte utf8 characters?

@trevnorris
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah crap. You're right. I should have caught that.

Please sign in to comment.