Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: nodejs > http > get > Example KO #37907

Closed
1 task
youpiwaza opened this issue Mar 25, 2021 · 4 comments
Closed
1 task

doc: nodejs > http > get > Example KO #37907

youpiwaza opened this issue Mar 25, 2021 · 4 comments
Labels
doc Issues and PRs related to the documentations. http Issues or PRs related to the http subsystem.

Comments

@youpiwaza
Copy link

📗 API Reference Docs Problem

  • Version: 14.16.0
  • Platform: Windows 64-bit, but not relevant
  • Subsystem: http

Location

Section of the site where the content exists

Affected URL(s):

Description

Concise explanation of the problem

Provided example doesn't work anymore, returns an error "Request failed. Status code: 301."

Sans titre

The provided url leads to http : http.get('http://nodejs.org/dist/index.json', (res) => {

but said resource get automatically redirected to it's https twin.

In the example, anything other than 200 is thrown away if (statusCode !== 200)

You need to set a new resource in http only (no redirection towards https) as the http module can't handle the https protocol ^^.

Cheers


  • I would like to work on this issue and
    submit a pull request.
@youpiwaza youpiwaza added the doc Issues and PRs related to the documentations. label Mar 25, 2021
@Ayase-252
Copy link
Member

Nice catch here!

As docs, it is always desirable to provide a working example. (although the example is illustrative enough for me personally :))/ The entire world is turning into https, there is not so much Public APIs which provides http service without authentication required. Moreover, such service is not guaranteed to work in the future.

Maybe we can spin up a local server to provide JSON service here? It could make things more controllable.

Also, I think this is a good first issue here. Would you like to submit a PR?

Thanks

@Ayase-252 Ayase-252 added the http Issues or PRs related to the http subsystem. label Mar 25, 2021
@youpiwaza
Copy link
Author

Hey there,

Thanks for your response. I didn't had the time to put my hands into the repo's code, so I don't haev a PR to submit sadly :(
(and I'm still kinda neeb with nodeJs :3)

The local server idea seems pretty good though ; or else simply switch the whole request to https0

Cheers

@hassaanp
Copy link
Contributor

hassaanp commented Apr 1, 2021

Hi,

If no one has taken this up yet, I'll be happy to push a quick fix.

@Ayase-252
Would adding a check for status code 301 for valid redirections be considered a valid fix for this?
Or, are we looking to host a local server that returns a placeholder json and let http get from the local server instead?

@Ayase-252
Copy link
Member

Ayase-252 commented Apr 1, 2021

@hassaanp

The example in question is to demostrate how to fetch a JSON response with "native" Node.js, therefore I prefer to host a very simple http server which outputs a JSON. Something like

const svr = http.createServer((req, res) => {
  res.writeHead(200, { "content-type": "application/json"})
  res.end(`{
    "data": "hello world"
  }`)
})
svr.listen(8000)

The 301 response from http://nodejs.org/dist/index.json actually redirects you to https version. I'm afraid it is not a valid fix to accommodate 301 response since this is a doc for http module.

Anyway, please feel free to take this 👍

hassaanp added a commit to hassaanp/node that referenced this issue Apr 2, 2021
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: nodejs#37907
@jasnell jasnell closed this as completed in 0d34767 Apr 6, 2021
targos pushed a commit that referenced this issue May 1, 2021
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. http Issues or PRs related to the http subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants