1

I am now using the https module to send some https POST requests.

A new requirement came up: the server's SSL support may be switched on or off. So I must use either https or http accordingly.

How do I send http requests using the https module? (the opposite would be helpful too)

According to the docs for function https.request(options[, callback]),

options.protocol Defaults to https:


I tried changing this to http:, for example,

 var options = {
        protocol: "http:",
        ...
    };
 https.request(options, function (res) { ... });

but got error Error: Protocol "http:" not supported. Expected "https:"

1 Answer 1

3

Just switch between modules. You could even do something like this:

(url.substr(0, 5) === 'https' ? https : http).request(...)
Sign up to request clarification or add additional context in comments.

Comments

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.