Strange behaviours when handling incorrect url
See original GitHub issueIs 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 reach127.0.0.1:80
. I had no clue why it happened, at first I didn’t know it was caused byaxios
. 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:
- Created 2 years ago
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 resultingparsed
object isnull
, and seems to fallback tolocalhost
in the deeper layers.There are two possible ways of dealing with this:
new URL(path)
instead ofurl.parse(path)
. The new API throws an errorInvalid 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?
I’ve run into this today. Axios does not handle invalid URLs at all, resulting in such errors as:
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.