Skip to content

Commit

Permalink
doc: sending http request to localhost to avoid https redirect
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Nitzan Uziely <[email protected]>
Reviewed-By: Juan José Arboleda <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
hassaanp authored and targos committed May 1, 2021
1 parent 56aaf70 commit aff0cd3
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit aff0cd3

Please sign in to comment.