You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases my app requires different defaults from the standard ones, and I've been dealing with this by putting something like this in the app's entry point:
let axiosDefaults = require('axios/lib/defaults');
axiosDefaults.baseURL = 'http://somesite.com';
My tests are run in Node and I want to override the baseURL so that axios will work with Nock even with relative URLs, but the code above doesn't work. Would it be reasonable to add this parameter to the defaultConfig when instantiating Axios? Or is there a better way I should be doing this? I have an example of what I mean here.
This sounds like a bug. I will have to dig in a bit deeper to confirm, but at a glance the code looks problematic. The Axios constructor holds onto a defaultConfig for each instance. Which means that if axios.defaults ever changes the default instance won't be modified.
@nickuraltsev But in that case every time you require axios you would need to call the constructor with the baseURL option, right? I was hoping for a way to override the defaults for my entire project.
I agree that config should be immutable for custom instances, but for the default axios instance changes to axios.default should change the config internally held by that instance.
To resolve this I think the defaults should be passed to the Axios constructor for the axios instance. Then change to axios.defaults = axios.defaultConfig so that modification will actually mean something.
Reacted by Young Zhao, Vincenzo Ferrari, Johan Olsson, cpprookie, Or Duan, kunokdev, Lucas Marques, mohamedAdel01, Daniel M., Matt Carlotta and 89 more
Reacted by Lucas Cardial, Anthony Gauthier, Sergio Fonseca Casao, Rudi, Noël Ruault, Verdi, Glogo, Márcio, Sanzhar Shakhmerden, Maciek Antek Papiewski and 2 more
Reacted by Daniel Neely, Rico Moorman, Or Duan, Zeno Jiricek, pleshevskiy, Chinwat K., scrumy, Lucas Cardial, Kohei🔧, Bengi and 24 more
@nickuraltsev you're correct that there is a difference between defaults and config. However axios.defaults should effect the config of the default axios instance in the case that no config is provided to override defaults. The problem with the current implementation is that defaultInstance = new Axios() is called the first time axios = require('axios') is called, before any chance is allowed to override axios.defaults. In this case axios.defaults should be mutable to allow developer to override default config.
@mzabriskie What if don't merge the defaults and defaultConfig in the constructor? We can merge the defaults, defaultConfig, and the request config in the request method instead. Thoughts?
importclientfrom'/rest-module'// request is made to http://localhost:3000/relative/pathclient().post('relative/path')
Reacted by Femina, Luan, Sanjay Yadav, Azad, Michael Capistrano and LIN
never mind! I realize I'm using the wrong property baseUrl instead baseURL
Reacted by Gabriel, Jeff Carbonella, Luan Guimarães, Brian McMillen, rodrix, Abdelmageed Sayed, Luddinus, Paul Morrison, Guilherme Latrova, Tom Scerri Jr and 60 more
Reacted by Adrian Wieprzkowicz, Hugo Torzuoli, Yuriy Chachora, Jonathan, Mark Omarov, lemurry, Yahor Ramanenka, Kuba, Bucky Schwarz, Yuxuan "Ethan" Chen and 23 more
Reacted by Paul Morrison, Fábio Antunes, Yuriy Chachora, Jonathan, lemurry, scrumy, Evgeni Atanasov, Kristijan Korać, Luca Di Liello, Bucky Schwarz and 12 more
Reacted by Bucky Schwarz, dmitry-ponizov, Thiago, Jonathan, Sharikov Vladislav, Jonathan Janssens, Elias Selvanovich, Orkhan Alikhanov, Linus Falck-Ytter and Fernando da Silva Sousa
Reacted by Jonathan, levi scott, Sharikov Vladislav, Elias Selvanovich, Linus Falck-Ytter and Fernando da Silva Sousa
Reacted by Jonathan, Sharikov Vladislav and Elias Selvanovich
Too late, but the way you can set only the root URL to baseURL like this
var url = ''
if (process.env.NODE_ENV === 'development') {
url = 'your domain' // you must hardcode it somewhere. In production you don't.
}
{
...
baseURL: url + '/' // in production the url variable will be set to the root url of your domain. '/' forces the basedURL to only use the root url with NO endpoints.
}
@Femina if you've got the Authorization: Bearer header in your API requests, you need to add the Access-Control-Allow headers into your API. For example, Node.js and Express it's like this:
For anyone still coming to this page for help with baseURL as the README states:
// baseURL will be prepended to url unless url is absolute.
essentially you must have a FQDN ie http:// or https:// in baseURL in order for it NOT to concatenate with the referrer URL (which to me is the desired behavior most people are looking for on this thread). Took me a few hours to sort that one.
Reacted by G. Pires, enkay, Nabil, Eriz, Alit Sawerigading and Alex Bachuk
Reacted by Ana Camila Fandiño de la Hoz and G. Pires
Activity
mzabriskie commentedon Dec 17, 2015
This sounds like a bug. I will have to dig in a bit deeper to confirm, but at a glance the code looks problematic. The
Axiosconstructor holds onto adefaultConfigfor each instance. Which means that ifaxios.defaultsever changes the default instance won't be modified.nickuraltsev commentedon Dec 17, 2015
@mzabriskie Right, but what's the point in making
defaults"mutable"? If you need to use custom config, you can just pass it to theAxiosconstructor.nathanharper commentedon Dec 17, 2015
@nickuraltsev But in that case every time you require axios you would need to call the constructor with the baseURL option, right? I was hoping for a way to override the defaults for my entire project.
mzabriskie commentedon Dec 17, 2015
I agree that config should be immutable for custom instances, but for the default axios instance changes to axios.default should change the config internally held by that instance.
To resolve this I think the defaults should be passed to the
Axiosconstructor for the axios instance. Then change toaxios.defaults = axios.defaultConfigso that modification will actually mean something.nickuraltsev commentedon Dec 17, 2015
@mzabriskie That makes sense. There are some differences between the structure of
defaultsandconfigthough. For example:nickuraltsev commentedon Dec 17, 2015
@nathanharper One solution is to create a small module like this:
and then use this new module in your code instead of requiring
axiosdirectly:mzabriskie commentedon Dec 21, 2015
@nickuraltsev you're correct that there is a difference between
defaultsandconfig. Howeveraxios.defaultsshould effect the config of the defaultaxiosinstance in the case that no config is provided to override defaults. The problem with the current implementation is thatdefaultInstance = new Axios()is called the first timeaxios = require('axios')is called, before any chance is allowed to overrideaxios.defaults. In this caseaxios.defaultsshould be mutable to allow developer to override default config.nickuraltsev commentedon Dec 21, 2015
@mzabriskie What if don't merge the
defaultsanddefaultConfigin the constructor? We can merge thedefaults,defaultConfig, and the request config in therequestmethod instead. Thoughts?mzabriskie commentedon Dec 24, 2015
This should be resolved with #183
nathanharper commentedon Dec 28, 2015
Awesome, that fixed my problem. Thanks for the fast turnaround on this!
mzabriskie commentedon Dec 28, 2015
Glad it works for you. I'll work on getting a new release out this week.
rkmax commentedon Mar 3, 2016
@mzabriskie this bug was fixed on the 0.9.1 version? because I'm experiencing it
I have a small module
later when I use I got a wrong baseUrl
rkmax commentedon Mar 3, 2016
never mind! I realize I'm using the wrong property
baseUrlinsteadbaseURL1 remaining item
chibicode commentedon Apr 21, 2016
@batusai513 I'm stuck trying to intercept axios requests with nock. This:
always says it didn't match. I swapped axios w/ isomorphic-fetch and it starts matching...
chibicode commentedon Apr 21, 2016
@batusai513 could be an issue w/ this (nock's matching against OPTIONS instead of POST)
#225
batusai513 commentedon Apr 22, 2016
@chibicode to bad, i use axios because the request and response interceptors, it would be cool to be able to use nock with this library
nathanharper commentedon May 3, 2016
@batusai513 I did manage to get nock to work with axios after this update got merged. Are you still having issues?
batusai513 commentedon May 4, 2016
@nathanharper yeah, i tried with v 0.10 and the test still hit the endpoint :/
batusai513 commentedon May 4, 2016
@nathanharper can you show me some gist of how you are implementing nock with axios, thanks.
monday1994 commentedon May 18, 2017
I have got following axios instance configuration:
let axiosInstance = axios.create({
baseURL: config.serverUrl
//where serverUrl i just my deployed server address 'http://myaddress.com'
});
module.exports = axiosInstance;
As you can see I am using correct format of baseURL (I mean URL instead of Url) but when I try to make simple get request to server I've got following base url http://localhost:3000/http://myaddress.com.
Can you tell me why ?
yasharma commentedon Jul 12, 2017
This is also working, you can also do like this, i also want to know that is it correct way to do??
fixing bug for issue axios#175 maybe axios#949 axios#443
blanet commentedon Oct 23, 2017
@monday1994 Did you solved it yet? I met the same problem, an extra "http:" inserted in my url...
Femina commentedon Jul 24, 2018
How can we use this with CORS ?
juanr2001 commentedon Oct 10, 2018
Too late, but the way you can set only the root URL to baseURL like this
lauri108 commentedon Nov 19, 2018
@Femina if you've got the
Authorization: Bearerheader in your API requests, you need to add the Access-Control-Allow headers into your API. For example, Node.js and Express it's like this:in the application's entry file.
efillman commentedon Jul 22, 2019
For anyone still coming to this page for help with baseURL as the README states:
//
baseURLwill be prepended tourlunlessurlis absolute.essentially you must have a FQDN ie http:// or https:// in baseURL in order for it NOT to concatenate with the referrer URL (which to me is the desired behavior most people are looking for on this thread). Took me a few hours to sort that one.