sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the Fetch API
MIT License
13.62k stars 363 forks source link

How can I use a proxy like in axios? #526

Closed MiniSuperDev closed 1 year ago

MiniSuperDev commented 1 year ago
axios({
  proxy: {
    protocol: 'https',
    host: '127.0.0.1',
    port: 9000,
    auth: {
      username: 'mikeymike',
      password: 'rapunz3l'
    }
})

Thanks

sindresorhus commented 1 year ago

If this is about Node.js, https://github.com/gajus/global-agent is usually the go to tool for this, unfortunately, it doesn't yet support: https://github.com/gajus/global-agent/issues/52

Proxy support is not something we plan to or can support built-in here.

transitive-bullshit commented 3 days ago

From what I've found, the easiest way to use a proxy with Node.js fetch (and hence ky) is via undici's experimental EnvHttpProxyAgent, which will respect the HTTP_PROXY, HTTPS_PROXY, and NO_PROXY environment variables.

npm install undici
import { EnvHttpProxyAgent } from 'undici'
import ky from 'ky'

const kyWithProxy = ky.extend({ dispatcher: new EnvHttpProxyAgent() })

This obviously won't work outside of Node.js, but it's still useful to know about.