doc: improved example for http.get
PR-URL: https://github.com/nodejs/node/pull/9065 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
parent
8a0b7ffac6
commit
cb87748023
@ -1437,16 +1437,47 @@ added: v0.3.6
|
||||
* Returns: {http.ClientRequest}
|
||||
|
||||
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.
|
||||
|
||||
Example:
|
||||
The `callback` is invoked with a single argument that is an instance of
|
||||
[`http.IncomingMessage`][]
|
||||
|
||||
JSON Fetching Example:
|
||||
|
||||
```js
|
||||
http.get('http://www.google.com/index.html', (res) => {
|
||||
console.log(`Got response: ${res.statusCode}`);
|
||||
// consume response body
|
||||
res.resume();
|
||||
http.get('http://nodejs.org/dist/index.json', (res) => {
|
||||
const statusCode = res.statusCode;
|
||||
const contentType = res.headers['content-type'];
|
||||
|
||||
let error;
|
||||
if (statusCode !== 200) {
|
||||
error = new Error(`Request Failed.\n` +
|
||||
`Status Code: ${statusCode}`);
|
||||
} else if (!/^application\/json/.test(contentType)) {
|
||||
error = new Error(`Invalid content-type.\n` +
|
||||
`Expected application/json but received ${contentType}`);
|
||||
}
|
||||
if (error) {
|
||||
console.log(error.message);
|
||||
// consume response data to free up memory
|
||||
res.resume();
|
||||
return;
|
||||
}
|
||||
|
||||
res.setEncoding('utf8');
|
||||
let rawData = '';
|
||||
res.on('data', (chunk) => rawData += chunk);
|
||||
res.on('end', () => {
|
||||
try {
|
||||
let parsedData = JSON.parse(rawData);
|
||||
console.log(parsedData);
|
||||
} catch (e) {
|
||||
console.log(e.message);
|
||||
}
|
||||
});
|
||||
}).on('error', (e) => {
|
||||
console.log(`Got error: ${e.message}`);
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user