Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error message for URLs missing protocol #3593

Closed
troyt-42 opened this issue Jan 25, 2021 · 1 comment
Closed

Better error message for URLs missing protocol #3593

troyt-42 opened this issue Jan 25, 2021 · 1 comment

Comments

@troyt-42
Copy link

troyt-42 commented Jan 25, 2021

Is your feature request related to a problem? Please describe.

My initial impression after reading #490 and 7c89704 is that Axios can handle URLs without protocol (ie. www.google.com) by defaulting to http. But when I try

axios({
  method: "get",
  url: "www.google.com",
})
  .then(function (response) {
    console.log(response);
  })
  .catch(function (e) {
    console.log(e);
  });

an error was thrown:

Error: connect ECONNREFUSED 127.0.0.1:80
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1146:16)
    at TCPConnectWrap.callbackTrampoline (internal/async_hooks.js:129:14) {
  errno: -61,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 80,
  config: {
    url: 'www.google.com',
    method: 'get',
    headers: {
      Accept: 'application/json, text/plain, */*',
      'User-Agent': 'axios/0.21.1'
    },
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 0,
    adapter: [Function: httpAdapter],
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    validateStatus: [Function: validateStatus],
    data: undefined
  },
...

To figure it out, I looked into the http adaptor and realized that 7c89704 was adding a default protocol for proxy only but not for the normal case.

So I thought about creating a PR to fix that but then I realized we are parsing the url using nodejs url module, but url can't handle URL without protocol, calling url.parse('www.google.com') generates:

Url {
  protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: null,
  query: null,
  pathname: 'www.google.com',
  path: 'www.google.com',
  href: 'www.google.com'
}

whose pathname and path are totally wrong. And if the url module can't parsing the url correct, I think there is little Axios can do to provide support. The best option here is to explicitly state that Axios doesn't support urls without protocols and throw an error with that message.

Describe the solution you'd like

Explicitly state in the doc that Axios doesn't support incomplete URLs (ie. www.google.com) and throw an error when receiving such URL through url or baseURL.

@joaoGabriel55
Copy link
Contributor

joaoGabriel55 commented Feb 4, 2021

Hello @taozytroy, I solved a similar problem in this PR: #3544

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants