Skip to content

Commit aff0cd3

Browse files
hassaanptargos
authored andcommitted
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 <[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]>
1 parent 56aaf70 commit aff0cd3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

doc/api/http.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -2287,7 +2287,7 @@ The `callback` is invoked with a single argument that is an instance of
22872287
JSON fetching example:
22882288

22892289
```js
2290-
http.get('http://nodejs.org/dist/index.json', (res) => {
2290+
http.get('http://localhost:8000/', (res) => {
22912291
const { statusCode } = res;
22922292
const contentType = res.headers['content-type'];
22932293

@@ -2322,6 +2322,16 @@ http.get('http://nodejs.org/dist/index.json', (res) => {
23222322
}).on('error', (e) => {
23232323
console.error(`Got error: ${e.message}`);
23242324
});
2325+
2326+
// Create a local server to receive data from
2327+
const server = http.createServer((req, res) => {
2328+
res.writeHead(200, { 'Content-Type': 'application/json' });
2329+
res.end(JSON.stringify({
2330+
data: 'Hello World!'
2331+
}));
2332+
});
2333+
2334+
server.listen(8000);
23252335
```
23262336

23272337
## `http.globalAgent`

0 commit comments

Comments
 (0)