5

I have server that accept only http (no https). And when I call an api, axios automatically change http to https.

I create axios instance here:

export const axiosAgent = axios.create({
    baseURL: "http://xxx.xxx.xxx/api/v1/",
});

But in browser http changes to https, so it become: https://xxx.xxx.xxx/api/v1/all

and I get net::ERR_NAME_NOT_RESOLVED

How can I prevent that?

UPDATE Request Header:

Provisional headers are shown
Accept: application/json, text/plain, */*
Access-Control-Allow-Origin: *
Referer: http://localhost:3000/patients
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36

and there is no response header.

and now I get net::ERR_SSL_PROTOCOL_ERROR

11
  • No doubt the server is redirecting your requests. The browser isn't going to do this on its own for XHR or Fetch. Commented May 15, 2020 at 21:14
  • @Brad But it woks in postman, or when I open that url in new tab.(With Allowing Insecure content ) Commented May 15, 2020 at 21:16
  • Can you open your browser's developer tools and show the full request/response headers for one of your API calls? Also, you shouldn't be getting net::ERR_NAME_NOT_RESOLVED just because of a protocol difference. Commented May 15, 2020 at 21:16
  • @Brad I update question Commented May 15, 2020 at 21:20
  • 2
    Full url is: https://xxx.xxx.xxx:8080/api/v1/operator/patient, But as I said, I create axios with http and in browser it changed to htttps. I don't know really what is happening.Sometimes net::ERR_NAME_NOT_RESOLVED and sometimes net::ERR_SSL_PROTOCOL_ERROR, but main point is to disable redirect to https. Commented May 15, 2020 at 21:27

3 Answers 3

11

I just remove <meta http-equiv="Content-Security-Policy" content="default-src 'self'"> from head in html. and problem solved. This meta tag change all http request to https one, so this is not related to axios.

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

Comments

0

In my case, I had the extension "HTTPS Everywhere" and that caused the redirection. Please check if you have any such extensions installed.

If Yes, disable the extension for time being.

Comments

-2

You can set url param with protocol. Example:

axios({
  method: 'GET',
  url: 'http://localhost:5050'
});

3 Comments

Here I just create an instance of axois. When I call api, I use axiosAgent.get(...)
Maybe you are trying to send request from https to http, and browser change protocal automatically ?
Yes, When Change Insecure content to Allow when I open it in new tab and It works but I my app not work.

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.