Skip to content

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

Description

@youpiwaza

📗 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.

Activity

  1. added
    docIssues and PRs related to Node.js documentation.
    on Mar 25, 2021
  2. Ayase-252 commented on Mar 25, 2021

    @Ayase-252
    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

  3. added
    httpIssues and PRs related to the http subsystem.
    on Mar 25, 2021
  4. youpiwaza commented on Mar 31, 2021

    @youpiwaza
    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

  5. hassaanp commented on Apr 1, 2021

    @hassaanp
    Contributor

    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?

  6. Ayase-252 commented on Apr 1, 2021

    @Ayase-252
    Member

    @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 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docIssues and PRs related to Node.js documentation.httpIssues and PRs related to the http subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions