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
Setting baseURL in default configuration #175
Comments
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 |
@mzabriskie Right, but what's the point in making |
@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 |
@mzabriskie That makes sense. There are some differences between the structure of // defaults
{
headers: {
common: {
'Accept': 'application/json, text/plain, */*'
},
patch: utils.merge(DEFAULT_CONTENT_TYPE),
}
} // config
{
headers: {
'X-Custom-Header': 'foobar'
}
} |
@nathanharper One solution is to create a small module like this: // client.js
var axios = require('axios');
var axiosInstance = axios.create({
baseURL: 'https://domain.com/foo/bar',
/* other custom settings */
});
module.exports = axiosInstance; and then use this new module in your code instead of requiring var client = require('./client');
client.get('relative/path') |
@nickuraltsev you're correct that there is a difference between |
@mzabriskie What if don't merge the |
This should be resolved with #183 |
Awesome, that fixed my problem. Thanks for the fast turnaround on this! |
Glad it works for you. I'll work on getting a new release out this week. |
@mzabriskie this bug was fixed on the 0.9.1 version? because I'm experiencing it I have a small module import axios from 'axios'
export default () => {
// __API__ = http://api.service.com
return axios.create({baseUrl: __API__})
} later when I use I got a wrong baseUrl import client from '/rest-module'
// request is made to http://localhost:3000/relative/path
client().post('relative/path') |
never mind! I realize I'm using the wrong property |
@nathanharper were you able to intercept axios requests with nock? |
@batusai513 I'm stuck trying to intercept axios requests with nock. This: nock(...)
.log(console.log) always says it didn't match. I swapped axios w/ isomorphic-fetch and it starts matching... |
@batusai513 could be an issue w/ this (nock's matching against OPTIONS instead of POST) |
@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 |
@batusai513 I did manage to get nock to work with axios after this update got merged. Are you still having issues? |
@nathanharper yeah, i tried with v 0.10 and the test still hit the endpoint :/ |
@nathanharper can you show me some gist of how you are implementing nock with axios, thanks. |
I have got following axios instance configuration: 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. |
import axios from 'axios'
window.axios.defaults.headers.common = {'X-Requested-With': 'XMLHttpRequest'}
window.axios.defaults.baseURL = (process.env.NODE_ENV !== 'production') ? 'http://localhost:9000/api/' : '' This is also working, you can also do like this, i also want to know that is it correct way to do?? |
@monday1994 Did you solved it yet? I met the same problem, an extra "http:" inserted in my url... |
How can we use this with CORS ? |
Too late, but the way you can set only the root URL to baseURL like this
|
@Femina if you've got the
in the application's entry file. |
For anyone still coming to this page for help with baseURL as the README states: // 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. |
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:
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.The text was updated successfully, but these errors were encountered: