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

Use Proxy with Fetch #1388

Closed
Singey32 opened this issue Apr 29, 2022 · 2 comments
Closed

Use Proxy with Fetch #1388

Singey32 opened this issue Apr 29, 2022 · 2 comments
Labels
bug Something isn't working fetch

Comments

@Singey32
Copy link

Problem

I'm unsure about how to use a proxy with fetch. I've found very little documentation about how to integrate a proxy and I seek to use separate proxies for any fetch request with username and password authentication if applicable.

Solution

As for a good solution I'd hope for a code example to perform a simple get request to a site (e.g. to ifconfig.me) using a proxy and instructions on how to include a username and password with that proxy.

@Singey32 Singey32 added the bug Something isn't working label Apr 29, 2022
@mcollina mcollina added the fetch label May 4, 2022
@KhafraDev
Copy link
Member

KhafraDev commented May 25, 2022

Possible with #1411, but if you are using fetch from node directly, you may need to wait for nodejs/node#43187 (if a decision to expose it is ever made).

If you are on node.js v18.2.0 or higher, you can also use node's fetch.

import { fetch, ProxyAgent } from 'undici'

const agent = new ProxyAgent('localhost:8888')

const response = await fetch('https://example.com', {
    dispatcher: agent
})

@sxzz
Copy link

sxzz commented Jul 26, 2022

TIPs: const agent = new ProxyAgent('localhost:8888') is not working (for me), remember to add http:// protocol.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fetch
Projects
None yet
Development

No branches or pull requests

4 participants