0

I have a frontend running on HTTPS locally with a local certificate. I'd like to hit a local backend that is on HTTP.

I added the meta tag to my index.html to allow for this:

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

...but axios refuses to do it:

        axios({
            method: 'post',
            baseURL: 'http://localhost:3000',
            url: '/test',
            data: {
                firstName: 'Will',
                lastName: 'Smith'
            }
        }).then((result) => {
            console.log('done!');
        }

Note that I explicitly add http to the baseUrl. Still, I get this:

POST https://localhost:3000/test net::ERR_SSL_PROTOCOL_ERROR

Axios still sends through https.

I tried using fetch:

       fetch('http://localhost:3000/test', {
            method : "POST",
            body: JSON.stringify({
                firstName: 'Finn',
                lastName: 'Williams'
            }),
        }).then((response) => {
            console.log('done! ' + response.json());
        ).catch((error) => {
            console.log('error');
        });

Then finally a raw XMLHttpRequest request:

        const http = new XMLHttpRequest();

        http.open("POST", 'http://localhost:3000/test', true);

        // Call a function when the state
        http.onreadystatechange = function () {
            if (http.readyState == 4 && http.status == 200) {
                alert(http.responseText);
            }
        }
        http.send(JSON.stringify({
            firstName: 'Finn',
            lastName: 'Williams'
        }));

But every time the browser seems to switch to HTTPS and it fails with that same error.

Is it possible to make this happen?

4
  • Isn't it possible to use just //localhost:3000/test so without http/https prefix, forcing the browser to decide? Commented Jul 2, 2021 at 19:49
  • That wouldn't work because the browser would pick HTTPS. The browser is running on HTTPS. I want to make an HTTP request. Commented Jul 2, 2021 at 20:14
  • It's not possible due to Same origin policy. (developer.mozilla.org/en-US/docs/Web/Security/…). Or you can have a server side solution to redirect https requests to http. Commented Jul 3, 2021 at 6:46
  • @PankajTanwar if you make it an answer I can accept it. Thanks! Commented Jul 5, 2021 at 16:57

1 Answer 1

1

It's not possible due to Same origin policy. Or you can have a server side solution to redirect https requests to http.

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.