question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Strange behaviours when handling incorrect url

See original GitHub issue

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

Today I was fixing a small bug caused by incorrect use of url. I specified url without http:// or https://. It took me hours to figure it out. There were two behaviours from axios distracted me from identifying the real issue.

  • axios tries to reach 127.0.0.1:80. I had no clue why it happened, at first I didn’t know it was caused by axios. So I created a mock server to run locally on port 80 and see where it was coming from, then this issue was gone and I was hit by another issue,
  • axios returns 400 error. This made me think the url I was calling returned 400.

To Reproduce:

const axios = require('axios');

axios({
  method: 'get',
  url: 'google.com/path', // url without https://
})
  .then(() => {
    // successful response
  })
  .catch((err) => {
    // when http://127.0.0.1:80 is not reachable, the err.message will be "connect ECONNREFUSED 127.0.0.1:80"
    // when http://127.0.0.1:80 is reachable, the err.message will be "Request failed with status code 400"
    console.error(err.message);
  });

Describe the solution you’d like

axios should throw proper error message (e.g.invalid url specified).

Describe alternatives you’ve considered

At the very least above behaviours should be avoided.

Additional context

  • Axios Version [0.24.0]
  • Node.js Version [14.x]
  • OS: [any]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ElmarFrerichscommented, Nov 15, 2021

Hi,

this behaviour emegerges because deep down, axios uses the deprecated url.parse of the node url package:

https://github.com/axios/axios/blob/c5fe05bdff82e0fa4ace9a832e75052e1ee297f0/lib/adapters/http.js#L120

This fails if no protocol is specified, and puts the remainings of the parsed string somewhere in the result object, but not in the correct fields. Thus, the field hostname of the resulting parsed object is null, and seems to fallback to localhost in the deeper layers.

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

There are two possible ways of dealing with this:

  • Catch an empty hostname and throw an error, as @PhantomRay proposed. This strategy might miss some other edge cases.
  • Fix the deprecated call. Node docs suggest to use new URL(path) instead of url.parse(path). The new API throws an error Invalid URL for the missing protocol. However, the resulting URL object does not match the legacy object of url.parse, so this would require a bigger refactoring of the following code.

I’d like to take a shot at this together with @Arne117 (and add a unit test), but we are not yet sure which approach would work better. Any suggestions?

1reaction
fregantecommented, Mar 23, 2022

I’ve run into this today. Axios does not handle invalid URLs at all, resulting in such errors as:

await axios.get('httdps://example.com')
// Error: Cannot read property 'replace' of null
await axios.getUri('httdps://example.com')
// TypeError: Cannot use 'in' operator to search for 'validateStatus' in httdps://example.com

And actually I’m surprised that the supposedly-equivalent getUri options parser handles it completely differently, which kind of defeats the purpose of exposing it publicly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IIS URL Rewrite - anchor in input causing strange behaviour.
When these links are clicked on the first time, the target location loads fine. However, if the very same link is clicked on...
Read more >
I'm seeing strange ActionLink behaviour, why is the url ...
yet when I hover over the link, the url displayed in the browser is http://localhost:44334/Upload . Where is the controller in this url?...
Read more >
strange behavior with children node url when website is ...
Hi, I'm using v8.9.1 and I just noticed something very strange. I have a multilingual site with the following Culture and Hostnames settings ......
Read more >
Solving some strange behaviour on Azure App Service with a ...
Let's say I'm working on a C# MVC site which gives some information about different countries, and I'd like to display the page's...
Read more >
WebUI.getUrl() strange behavior with home URL
... an 'incorrect' URL that must be redirected to a correct one; I use WebUI.getUrl() to get the de facto URL; I check...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found