Skip to content

Setting baseURL in default configuration #175

Closed

Description

@nathanharper

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.

Activity

  1. mzabriskie commented on Dec 17, 2015

    @mzabriskie
    Member

    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.

  2. nickuraltsev commented on Dec 17, 2015

    @nickuraltsev
    Contributor

    @mzabriskie Right, but what's the point in making defaults "mutable"? If you need to use custom config, you can just pass it to the Axios constructor.

  3. nathanharper commented on Dec 17, 2015

    @nathanharper
    Author

    @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.

  4. mzabriskie commented on Dec 17, 2015

    @mzabriskie
    Member

    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.

  5. nickuraltsev commented on Dec 17, 2015

    @nickuraltsev
    Contributor

    @mzabriskie That makes sense. There are some differences between the structure of defaults and config though. For example:

    // defaults
    {
      headers: {
        common: {
          'Accept': 'application/json, text/plain, */*'
        },
        patch: utils.merge(DEFAULT_CONTENT_TYPE),
      }
    }
    // config
    {
      headers: {
        'X-Custom-Header': 'foobar'
      }
    }
  6. nickuraltsev commented on Dec 17, 2015

    @nickuraltsev
    Contributor

    @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 axios directly:

    var client = require('./client');
    
    client.get('relative/path')
  7. mzabriskie commented on Dec 21, 2015

    @mzabriskie
    Member

    @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.

  8. nickuraltsev commented on Dec 21, 2015

    @nickuraltsev
    Contributor

    @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?

  9. mzabriskie commented on Dec 24, 2015

    @mzabriskie
    Member

    This should be resolved with #183

  10. nathanharper commented on Dec 28, 2015

    @nathanharper
    Author

    Awesome, that fixed my problem. Thanks for the fast turnaround on this!

  11. mzabriskie commented on Dec 28, 2015

    @mzabriskie
    Member

    Glad it works for you. I'll work on getting a new release out this week.

  12. rkmax commented on Mar 3, 2016

    @rkmax

    @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')
  13. rkmax commented on Mar 3, 2016

    @rkmax

    never mind! I realize I'm using the wrong property baseUrl instead baseURL

  14. 1 remaining item

  15. chibicode commented on Apr 21, 2016

    @chibicode

    @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...

  16. chibicode commented on Apr 21, 2016

    @chibicode

    @batusai513 could be an issue w/ this (nock's matching against OPTIONS instead of POST)
    #225

  17. batusai513 commented on Apr 22, 2016

    @batusai513

    @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

  18. nathanharper commented on May 3, 2016

    @nathanharper
    Author

    @batusai513 I did manage to get nock to work with axios after this update got merged. Are you still having issues?

  19. batusai513 commented on May 4, 2016

    @batusai513

    @nathanharper yeah, i tried with v 0.10 and the test still hit the endpoint :/

  20. batusai513 commented on May 4, 2016

    @batusai513

    @nathanharper can you show me some gist of how you are implementing nock with axios, thanks.

  21. monday1994 commented on May 18, 2017

    @monday1994

    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 ?

  22. yasharma commented on Jul 12, 2017

    @yasharma
    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??

  23. added a commit that references this issue on Aug 4, 2017
  24. blanet commented on Oct 23, 2017

    @blanet

    @monday1994 Did you solved it yet? I met the same problem, an extra "http:" inserted in my url...

  25. Femina commented on Jul 24, 2018

    @Femina

    How can we use this with CORS ?

  26. juanr2001 commented on Oct 10, 2018

    @juanr2001

    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.
    }
    
  27. lauri108 commented on Nov 19, 2018

    @lauri108

    How can we use this with CORS ?

    @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:

    app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*")
    res.header("Access-Control-Allow-Headers", "Authorization, Origin, X-Requested-With, Content-Type, Accept")
    res.header("Access-Control-Allow-Methods", "GET,POST")
    next()
    })

    in the application's entry file.

  28. efillman commented on Jul 22, 2019

    @efillman

    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.

  29. locked and limited conversation to collaborators on May 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions