From aff0cd3ea6dcbaab531ff28966e8931dc0d75604 Mon Sep 17 00:00:00 2001 From: Hassaan Pasha Date: Fri, 2 Apr 2021 11:16:09 +0500 Subject: [PATCH] doc: sending http request to localhost to avoid https redirect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/issues/37907 PR-URL: https://github.com/nodejs/node/pull/38036 Reviewed-By: Antoine du Hamel Reviewed-By: Nitzan Uziely Reviewed-By: Juan José Arboleda Reviewed-By: Rich Trott Reviewed-By: James M Snell --- doc/api/http.md | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index a50bd445c17faa..12ae7b5e5167a6 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -2287,7 +2287,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']; @@ -2322,6 +2322,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`