4

I am trying to make a request in node. Here is my code:

fetch("http://www.test.com", options).catch(err => {
    console.error(err);

    return err;
  });

Here is the error that I get. TypeError: Only HTTP(S) protocols are supported

I have tried to create a new agent and pass it in the options like this:

const httpAgent = new http.Agent({
    keepAlive: true
});
const httpsAgent = new https.Agent({
    keepAlive: true
});

const options = {
    agent: function (_parsedURL) {
        if (_parsedURL.protocol == 'http:') {
            return httpAgent;
        } else {
            return httpsAgent;
        }
    }
}

I still get the same error.

When I run this in the client, I can send an http or an https request to the same fetch and there isn't an issue. Apparently it is different on with node.

What is the best way to handle this?

2
  • This looks very weird. Are you sure it is saved? Has webpack recompiled? Does node need to restart? Are you sure the error is for that line? I've used node-fetch a ton myself, and this should work. Commented Dec 6, 2019 at 1:46
  • i don't know, they force it to be https github.com/node-fetch/node-fetch/blob/master/src/… Commented Jan 27, 2020 at 8:43

1 Answer 1

8

Check if you've passed a url with a protocol. Error "Only HTTP(S) protocols are supported" means that there's neither HTTP nor HTTPS protocol found.

Sign up to request clarification or add additional context in comments.

1 Comment

Spot on! I was passing through "localhost:3001", when I added the http:// at the beginning I didn't get this error anymore.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.