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

GET request with proxy doesn't work on v1.1.2 #5079

Closed
geomad opened this issue Oct 9, 2022 · 4 comments · Fixed by #5097
Closed

GET request with proxy doesn't work on v1.1.2 #5079

geomad opened this issue Oct 9, 2022 · 4 comments · Fixed by #5097

Comments

@geomad
Copy link

geomad commented Oct 9, 2022

Describe the bug

Making requests with proxy and an agent is broken on v1.x while its working on v0.27

To Reproduce

const axios = require("axios")
const https = require('https');

const agent = new https.Agent({
    rejectUnauthorized: false,
    keepAlive: true,
    maxSockets: Infinity,
    maxFreeSockets: 256,
})

let options = {
    proxy: {
        protocol: 'https',
        host: proxy.host,
        port: 89,
        auth: {
            username: proxy.username,
            password: proxy.password
        }
    },
    httpsAgent: agent
}

const response = await axios.get("https://google.com", options)

The above code while workling on 0.27, now returns

AssertionError [ERR_ASSERTION]: protocol mismatch
 {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: 'https',
  expected: 'https:',
  operator: '=='
}

If protocol: 'https' is replaced with protocol: 'https:' there is still an error

AxiosError: connect ECONNREFUSED ::1:89
    at AxiosError.from (C:\dev\node_modules\axios\dist\node\axios.cjs:725:14)
    at RedirectableRequest.handleRequestError (C:\dev\node_modules\axios\dist\node\axios.cjs:2465:25)
    at RedirectableRequest.emit (node:events:539:35)
    at eventHandlers.<computed> (C:\dev\node_modules\follow-redirects\index.js:14:24)
    at ClientRequest.emit (node:events:527:28)
    at TLSSocket.socketErrorListener (node:_http_client:462:9)
    at TLSSocket.emit (node:events:527:28)
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  port: 89,
  address: '::1',
  syscall: 'connect',
  code: 'ECONNREFUSED',
  errno: -4078,
  config: {
    transitional: {
      silentJSONParsing: true,
      forcedJSONParsing: true,
      clarifyTimeoutError: false
    },
    adapter: [Function: httpAdapter],
    transformRequest: [ [Function: transformRequest] ],
    transformResponse: [ [Function: transformResponse] ],
    timeout: 6000,
    xsrfCookieName: 'XSRF-TOKEN',
    xsrfHeaderName: 'X-XSRF-TOKEN',
    maxContentLength: -1,
    maxBodyLength: -1,
    env: { FormData: [Function], Blob: [class Blob] },
    validateStatus: [Function: validateStatus],
    headers: AxiosHeaders {
      'User-Agent': 'axios/1.1.2',
      'Accept-Encoding': 'gzip, deflate, br',
      [Symbol(defaults)]: [Object]
    },
    proxy: {
      protocol: 'https:',
      host: <proxy hostname>,
      port: '89',
      auth: <proxy auth>
    },
    httpsAgent: Agent {
      _events: [Object: null prototype],
      _eventsCount: 2,
      _maxListeners: undefined,
      defaultPort: 443,
      protocol: 'https:',
      options: [Object: null prototype],
      requests: [Object: null prototype] {},
      sockets: [Object: null prototype],
      freeSockets: [Object: null prototype] {},
      keepAliveMsecs: 1000,
      keepAlive: true,
      maxSockets: Infinity,
      maxFreeSockets: 256,
      scheduling: 'lifo',
      maxTotalSockets: Infinity,
      totalSocketCount: 1,
      maxCachedSessions: 100,
      _sessionCache: [Object],
      [Symbol(kCapture)]: false
    },
    method: 'get',
    url: 'https://google.com',
    data: undefined
  },
  request: <ref *1> 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,
      afterWriteTickInfo: null,
      buffered: [],
      bufferedIndex: 0,
      allBuffers: true,
      allNoop: true,
      pendingcb: 0,
      constructed: true,
      prefinished: false,
      errorEmitted: false,
      emitClose: true,
      autoDestroy: true,
      errored: null,
      closed: false,
      closeEmitted: false,
      [Symbol(kOnFinished)]: []
    },
    _events: [Object: null prototype] {
      response: [Function: handleResponse],
      error: [Function: handleRequestError],
      socket: [Array]
    },
    _eventsCount: 3,
    _maxListeners: undefined,
    _options: {
      maxRedirects: 21,
      maxBodyLength: Infinity,
      protocol: 'https:',
      path: 'https://google.com/',
      method: 'GET',
      headers: [Object: null prototype],
      agents: [Object],
      auth: undefined,
      beforeRedirect: [Function: dispatchBeforeRedirect],
      beforeRedirects: [Object],
      hostname: '::1',
      port: '89',
      host: undefined,
      agent: [Agent],
      nativeProtocols: [Object],
      pathname: 'https://google.com/'
    },
    _ended: true,
    _ending: true,
    _redirectCount: 0,
    _redirects: [],
    _requestBodyLength: 0,
    _requestBodyBuffers: [],
    _onNativeResponse: [Function (anonymous)],
    _currentRequest: ClientRequest {
      _events: [Object: null prototype],
      _eventsCount: 7,
      _maxListeners: undefined,
      outputData: [],
      outputSize: 0,
      writable: true,
      destroyed: false,
      _last: true,
      chunkedEncoding: false,
      shouldKeepAlive: true,
      maxRequestsOnConnectionReached: false,
      _defaultKeepAlive: true,
      useChunkedEncodingByDefault: false,
      sendDate: false,
      _removedConnection: false,
      _removedContLen: false,
      _removedTE: false,
      _contentLength: 0,
      _hasBody: true,
      _trailer: '',
      finished: true,
      _headerSent: true,
      _closed: false,
      socket: [TLSSocket],
      _header: 'GET https://google.com/ HTTP/1.1\r\n' +
        'Accept: application/json, text/plain, */*\r\n' +
        'User-Agent: axios/1.1.2\r\n' +
        'Accept-Encoding: gzip, deflate, br\r\n' +
        'Proxy-Authorization: Basic <token>\r\n' +
        'host: google.com\r\n' +
        'Connection: keep-alive\r\n' +
        '\r\n',
      _keepAliveTimeout: 0,
      _onPendingData: [Function: nop],
      agent: [Agent],
      socketPath: undefined,
      method: 'GET',
      maxHeaderSize: undefined,
      insecureHTTPParser: undefined,
      path: 'https://google.com/',
      _ended: false,
      res: null,
      aborted: false,
      timeoutCb: null,
      upgradeOrConnect: false,
      parser: null,
      maxHeadersCount: null,
      reusedSocket: false,
      host: '::1',
      protocol: 'https:',
      _redirectable: [Circular *1],
      [Symbol(kCapture)]: false,
      [Symbol(kNeedDrain)]: false,
      [Symbol(corked)]: 0,
      [Symbol(kOutHeaders)]: [Object: null prototype],
      [Symbol(kUniqueHeaders)]: null
    },
    _currentUrl: 'https://google.com/',
    _timeout: null,
    [Symbol(kCapture)]: false
  },
  cause: Error: connect ECONNREFUSED ::1:89
      at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1229:16) {
    errno: -4078,
    code: 'ECONNREFUSED',
    syscall: 'connect',
    address: '::1',
    port: 89
  }
}

Expected behavior

v1 works like v0.27

Environment

  • Axios Version v1.1.2
  • Node.js Version v18.10.0
  • OS: windows & linux
@sabindev
Copy link

same issue here

@ppati000
Copy link
Contributor

ppati000 commented Oct 10, 2022

@geomad @sabindev The issue seems to be the host. Axios is trying to connect to ::1, where your proxy might not be listening. To fix this you can use hostname instead of host in the proxy config.

However, both the colon (:) in the protocol and requiring hostname instead of host are breaking changes in 1.x. I will attempt to create a pull request to allow both fields and make the colon character optional.

The working config for Axios 1.1.2 should be:

    proxy: {
        protocol: 'https:',
        hostname: proxy.host,
        // ... other options
    }

@geomad
Copy link
Author

geomad commented Oct 10, 2022

@geomad @sabindev The issue seems to be the host. Axios is trying to connect to ::1, where your proxy might not be listening. To fix this you can use hostname instead of host in the proxy config.

However, both the colon (:) in the protocol and requiring hostname instead of host are breaking changes in 1.x. I will attempt to create a pull request to allow both fields and make the colon character optional.

The working config for Axios 1.1.2 should be:

    proxy: {
        protocol: 'https:',
        hostname: proxy.host,
        // ... other options
    }

I can confirm this is working! Thanks!

@ppati000
Copy link
Contributor

@geomad Great to hear that! I have now prepared a small pull request to enhance backwards compatibility with 0.x versions: #5097

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants