Double base url appended
See original GitHub issueUsing /v1/api via:
window.axios.defaults.baseURL = '/v1/api/'
On error results in ‘/v1/api/v1/api’ call
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:10 (2 by maintainers)
Top Results From Across the Web
Axios.get repeating base URL twice - Stack Overflow
I'm trying to use axios.get() to access the backend of a site and returns JSON . However the server returns:
Read more >Full base url appended after base url when reaching page
The first thing you can check is the website's base URL from the database. To do that, log in to the database and...
Read more >Web Page
Depending on the relative URL convention you use, the Full URL inserts all or part of the base path and appends the relative...
Read more >URL() - Web APIs - MDN Web Docs - Mozilla
A string representing the base URL to use in cases where url is a relative URL. If not specified, it defaults to undefined...
Read more >visit - Cypress Documentation
The URL to visit. This value will be appended to the baseUrl if one is configured. Behaves the same as the url argument....
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
@riksnelders , others: I noticed in the code posted in the OP has a relative URL, not an absolute URL.
Axios is making a lot of assumptions in their code that the URL MUST be absolute. An easy fix may be to set it to
location.origin + '/v1/your/api/base/here'
.After making this change in our app, I noticed this bug went away.
Aside, this is actually a core axios expectation.
This is actually spelled out in the documentation, but its extremely easy to overlook (its in the comment above the
baseUrl
config in the code:https://github.com/axios/axios#request-config
I think if enough heads work on the problem, they can clean it up and have
baseUrl
ALSO support relative paths, as that expectation works cleanly with just core axios. The sloppy handling of config values manifests an issue when trying to add the retry functionality.Facing the same issue, and is not related to
retry-axios
but toaxios
. If you look atdispatchRequest.js
in the axios source code you’ll see near line 25.If you don’t use an absolute URL as
baseURL
, theconfig.url
will be always combined with thebaseURL
.This means that… if you do
window.axios.defaults.baseURL = '/v1/api/'
, the first time you execute a request, such asaxios.get('/user')
the actualurl
will be[config.baseURL]
+[config.url]
='/v1/api/
+user'
='/v1/api/user'
.When that request is retried the code of dispatchRequest.js will be repeated, and now the value of
url
is already “prepended” with the baseURL. So theconfig.url
will end up with[baseURL]
+[url]
='/v1/api/
+/vi/api/user'
='/v1/api/v1/api/user'
.Solutions
baseURL
config.URL
if the request is being retried