Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -1354,16 +1354,24 @@ added: v0.3.6
-->

Since most requests are GET requests without bodies, Node.js provides this
convenience method. The only difference between this method and [`http.request()`][]
is that it sets the method to GET and calls `req.end()` automatically.
convenience method. The only difference between this method and
[`http.request()`][] is that it sets the method to GET and calls `req.end()`
automatically. Note that response data must be consumed in the callback
for reasons stated in [`http.ClientRequest`][] section.

`callback` takes one argument which is an instance of [`http.IncomingMessage`][]
Copy link
Member

Choose a reason for hiding this comment

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

Please make this a proper sentence, e.g.:

The `callback` is invoked with a single argument that is an instance of
[`http.IncomingMessage`][]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great sentence. Thanks.


Example:

```js
http.get('http://www.google.com/index.html', (res) => {
console.log(`Got response: ${res.statusCode}`);
// consume response body
res.resume();
console.log(`STATUS: ${res.statusCode}`);
res.setEncoding('utf8');
let aggregatedData = '';
res.on('data', (chunk) => aggregatedData += chunk);
Copy link
Member

Choose a reason for hiding this comment

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

Concatenating strings isn't a good method of getting the response body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ChALkeR what's wrong with concatenating strings, and how it should be done then?

Copy link
Contributor

@mscdex mscdex Oct 12, 2016

Choose a reason for hiding this comment

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

Concatenating strings is fine if the response is text, but for binary it wouldn't work. Using binary from the get-go would work for both cases (e.g. pushing Buffers to an array, keeping a total byte count, and calling Buffer.concat() at the end) but still you'd have to know what to do with the end result (either converting to string or whatever), so it's kind of tricky for a generic example.

If we want to make it more concrete, we could turn it into a JSON fetching example. That way we could show checking the response code for 200, checking that the Content-Type starts with 'application/json', and then concatenating and parsing the response?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

JSON fetching example sound good.

res.on('end', () => {
console.log(`Message body: ${aggregatedData}`);
});
}).on('error', (e) => {
console.log(`Got error: ${e.message}`);
});
Expand Down