Skip to content
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

BaseURL not being used #1470

Closed
tiarebalbi opened this issue Apr 10, 2018 · 13 comments
Closed

BaseURL not being used #1470

tiarebalbi opened this issue Apr 10, 2018 · 13 comments

Comments

@tiarebalbi
Copy link

Summary

I'm having a problem with the defaults property. When an instance is created I'm setting the baseUrl, checking the defaults value I can see that the baseUrl has been set but once I try to send a single request axios is not using the baseUrl set previously.

Code used to request
const config = {
        timeout: 1500,
        baseUrl: 'https://reqres.in/api',
        ...defaults,
};

const instance = axios.create(config);
try {
        const response = await instance.get('/users?page=2');
        console.log(response);
} catch (e) {
        console.error(e);
}

Note in the end of the response: _currentUrl: 'http:/users?page=2' If I change the get method to have the full URL the request works.

Response
{
    Error: connect ECONNREFUSED 127.0 .0 .1: 80
    at TCPConnectWrap.afterConnect[as oncomplete](net.js: 1173: 14)
    errno: 'ECONNREFUSED',
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '127.0.0.1',
    port: 80,
    config: {
        adapter: [Function: httpAdapter],
        transformRequest: {
            '0': [Function: transformRequest]
        },
        transformResponse: {
            '0': [Function: transformResponse]
        },
        timeout: 1500,
        xsrfCookieName: 'XSRF-TOKEN',
        xsrfHeaderName: 'X-XSRF-TOKEN',
        maxContentLength: -1,
        validateStatus: [Function: validateStatus],
        headers: {
            Accept: 'application/json, text/plain, */*',
            'User-Agent': 'axios/0.18.0'
        },
        method: 'get',
        baseUrl: 'https://reqres.in/api',
        url: '/users?page=2',
        data: undefined
    },
    request: Writable {
        _writableState: WritableState {
            objectMode: false,
            highWaterMark: 16384,
            finalCalled: false,
            needDrain: false,
            ending: false,
            ended: false,
            finished: false,
            destroyed: false,
            decodeStrings: true,
            defaultEncoding: 'utf8',
            length: 0,
            writing: false,
            corked: 0,
            sync: true,
            bufferProcessing: false,
            onwrite: [Function: bound onwrite],
            writecb: null,
            writelen: 0,
            bufferedRequest: null,
            lastBufferedRequest: null,
            pendingcb: 0,
            prefinished: false,
            errorEmitted: false,
            bufferedRequestCount: 0,
            corkedRequestsFree: [Object]
        },
        writable: true,
        _events: {
            response: [Function: handleResponse],
            error: [Function: handleRequestError]
        },
        _eventsCount: 2,
        _maxListeners: undefined,
        _options: {
            protocol: 'http:',
            maxRedirects: 21,
            maxBodyLength: 10485760,
            path: '/users?page=2',
            method: 'get',
            headers: [Object],
            agent: undefined,
            auth: undefined,
            hostname: null,
            port: null,
            nativeProtocols: [Object],
            pathname: '/users',
            search: '?page=2'
        },
        _redirectCount: 0,
        _requestBodyLength: 0,
        _requestBodyBuffers: [],
        _onNativeResponse: [Function],
        _currentRequest: ClientRequest {
            _events: [Object],
            _eventsCount: 6,
            _maxListeners: undefined,
            output: [],
            outputEncodings: [],
            outputCallbacks: [],
            outputSize: 0,
            writable: true,
            _last: true,
            upgrading: false,
            chunkedEncoding: false,
            shouldKeepAlive: false,
            useChunkedEncodingByDefault: false,
            sendDate: false,
            _removedConnection: false,
            _removedContLen: false,
            _removedTE: false,
            _contentLength: 0,
            _hasBody: true,
            _trailer: '',
            finished: true,
            _headerSent: true,
            socket: [Socket],
            connection: [Socket],
            _header: 'GET /users?page=2 HTTP/1.1\r\nAccept: application/json, text/plain, */*\r\nUser-Agent: axios/0.18.0\r\nHost: localhost\r\nConnection: close\r\n\r\n',
            _onPendingData: [Function: noopPendingOutput],
            agent: [Agent],
            socketPath: undefined,
            timeout: undefined,
            method: 'GET',
            path: '/users?page=2',
            _ended: false,
            res: null,
            aborted: undefined,
            timeoutCb: null,
            upgradeOrConnect: false,
            parser: null,
            maxHeadersCount: null,
            _redirectable: [Circular],
            [Symbol(isCorked)]: false,
            [Symbol(outHeadersKey)]: [Object]
        },
        _currentUrl: 'http:/users?page=2'
    },
    response: undefined
}

Context

  • axios version: 0.18.0
  • Environment: node v9.9.0, chrome 65.0.3325.181, Mac OSX 10.13.4 (17E199)
@tiarebalbi
Copy link
Author

Please, IGNORE THIS ISSUE. Found the problem:

I was setting baseUrl but it should be baseURL.

@Joaaofiilho
Copy link

Funny enough, that was my problem too. I just realize the mistake after your comment, thank you Mister @tiarebalbi , you were very useful ahahahahahaha

@bchellingworth
Copy link

Im sure this as been changed, I just copied some old code with baseUrl that was working fine and now doesn't work in a more recent project... Ah well.

@lou-bi
Copy link

lou-bi commented Jun 12, 2019

Lifesaver, thanks !

@heikkiket
Copy link

Thank you very much! This was the exact same problem for me as well. Google search "axios baseurl not working" was totally worth it :D

@AdamsEric
Copy link

Same problem here... thanks!

@thiagomotaz
Copy link

dear God, same problem here hahahaha, camelCase sucks

@alex-w0
Copy link

alex-w0 commented Mar 29, 2020

How can so many people make the same mistake? I've been looking for over an hour now ^^
Was there a reason to write URL in uppercase?

@kemicofa
Copy link

kemicofa commented Apr 1, 2020

Using baseURL is not working. It keeps sending requests to port 80

@kemicofa
Copy link

kemicofa commented Apr 1, 2020

When I log the instance I'm getting:

defaults: {
        headers: {
          common: [Object],
          delete: {},
          get: {},
          head: {},
          post: [Object],
          put: [Object],
          patch: [Object],
          'Content-Type': 'application/json; charset=utf-8'
        },
        baseURL: 'http://localhost:3000',
        transformRequest: [ [Function: transformRequest] ],
        transformResponse: [ [Function: transformResponse] ],
        timeout: 1000,
        adapter: [Function: xhrAdapter],
        xsrfCookieName: 'XSRF-TOKEN',
        xsrfHeaderName: 'X-XSRF-TOKEN',
        maxContentLength: -1,
        validateStatus: [Function: validateStatus]
      }

For the config.

@determin1st
Copy link

well, probably there could be an alias of baseUrl to baseURL

@alex-w0
Copy link

alex-w0 commented Apr 2, 2020

@kemicofa Can you also post an example of the request or even better create a small codepen?

@mauchchhash
Copy link

Had the same problem. :P

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests