From 2da6327f1237ec44ccdcd71636f0848dffe3ca12 Mon Sep 17 00:00:00 2001 From: Hassaan Pasha Date: Fri, 2 Apr 2021 11:16:09 +0500 Subject: [PATCH 1/3] 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: https://github.com/nodejs/node/issues/37907 --- 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 61bc880f43eb8e..599138db0b5a86 100644 --- a/doc/api/http.md +++ b/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(`{ + 'data': 'hello world' + }`); +}); + +server.listen(8000); ``` ## `http.globalAgent` From 3b346d40c285676f567f99f72432c181901ac26b Mon Sep 17 00:00:00 2001 From: Hassaan Pasha Date: Fri, 2 Apr 2021 15:15:52 +0500 Subject: [PATCH 2/3] Update doc/api/http.md Co-authored-by: Antoine du Hamel --- doc/api/http.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 599138db0b5a86..4e793628cd62e9 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -2759,9 +2759,9 @@ http.get('http://localhost:8000/', (res) => { // Create a local server to receive data from const server = http.createServer((req, res) => { res.writeHead(200, { 'content-type': 'application/json' }); - res.end(`{ - 'data': 'hello world' - }`); + res.end(JSON.stringify({ + data: 'Hello World!' + })); }); server.listen(8000); From 3b2f38c16b0985baca9348529e460315e17ef835 Mon Sep 17 00:00:00 2001 From: Hassaan Pasha Date: Fri, 2 Apr 2021 20:52:28 +0500 Subject: [PATCH 3/3] Update doc/api/http.md Co-authored-by: Antoine du Hamel --- doc/api/http.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/api/http.md b/doc/api/http.md index 4e793628cd62e9..d3f773f048a18b 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -2758,7 +2758,7 @@ http.get('http://localhost:8000/', (res) => { // Create a local server to receive data from const server = http.createServer((req, res) => { - res.writeHead(200, { 'content-type': 'application/json' }); + res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ data: 'Hello World!' }));