X Tutup
The Wayback Machine - https://web.archive.org/web/20210814015736/https://github.com/nodejs/node/commit/0d34767c4c
Skip to content
Permalink
Browse files
doc: sending http request to localhost to avoid https redirect
In the JSON fetching example, http.get request is being sent to
an http url that redirects to https. This causes the http.get
request to fail. To avoid redirect errors, a local http server
is set up that returns a json response.

Fixes: #37907

PR-URL: #38036
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Nitzan Uziely <linkgoron@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
hassaanp authored and jasnell committed Apr 6, 2021
1 parent f851efd commit 0d34767c4cb84b067b08505c3d98215e0adeee5d
Showing with 11 additions and 1 deletion.
  1. +11 −1 doc/api/http.md
@@ -2720,7 +2720,7 @@ The `callback` is invoked with a single argument that is an instance of
JSON fetching example:

```js
http.get('http://nodejs.org/dist/index.json', (res) => {
http.get('http://localhost:8000/', (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
@@ -2755,6 +2755,16 @@ http.get('http://nodejs.org/dist/index.json', (res) => {
}).on('error', (e) => {
console.error(`Got error: ${e.message}`);
});
// Create a local server to receive data from
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({
data: 'Hello World!'
}));
});
server.listen(8000);
```

## `http.globalAgent`

0 comments on commit 0d34767

Please sign in to comment.
X Tutup