-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
doc: better example for http.get #9065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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`][] | ||
|
|
||
| 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); | ||
|
||
| res.on('end', () => { | ||
| console.log(`Message body: ${aggregatedData}`); | ||
| }); | ||
| }).on('error', (e) => { | ||
| console.log(`Got error: ${e.message}`); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
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.:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great sentence. Thanks.