Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
    Overview
    HTTP & Networking

    Proxy HTTP requests using fetch()

    In Bun,fetch supports sending requests through an HTTP or HTTPS proxy. This is useful on corporate networks or when you need to ensure a request is sent through a specific IP address.
    https://mintcdn.com/bun-1dd33a4e/Hq64iapoQXHbYMEN/icons/typescript.svg?fit=max&auto=format&n=Hq64iapoQXHbYMEN&q=85&s=c6cceedec8f82d2cc803d7c6ec82b240proxy.ts
    await fetch("https://example.com", {  // The URL of the proxy server  proxy: "https://username:[email protected]:8080",});

    Theproxy option can be a URL string or an object withurl and optionalheaders. The URL can include the username and password if the proxy requires authentication. It can behttp:// orhttps://.

    Custom proxy headers

    To send custom headers to the proxy server (useful for proxy authentication tokens, custom routing, etc.), use the object format:
    https://mintcdn.com/bun-1dd33a4e/Hq64iapoQXHbYMEN/icons/typescript.svg?fit=max&auto=format&n=Hq64iapoQXHbYMEN&q=85&s=c6cceedec8f82d2cc803d7c6ec82b240proxy-headers.ts
    await fetch("https://example.com", {  proxy: {    url: "https://proxy.example.com:8080",    headers: {      "Proxy-Authorization": "Bearer my-token",      "X-Proxy-Region": "us-east-1",    },  },});
    Theheaders property accepts a plain object or aHeaders instance. These headers are sent directly to the proxy server inCONNECT requests (for HTTPS targets) or in the proxy request (for HTTP targets).If you provide aProxy-Authorization header, it will override any credentials specified in the proxy URL.

    Environment variables

    You can also set the$HTTP_PROXY or$HTTPS_PROXY environment variable to the proxy URL. This is useful when you want to use the same proxy for all requests.
    terminal
    HTTPS_PROXY=https://username:[email protected]:8080 bun run index.ts

    Was this page helpful?

    ⌘I

    [8]ページ先頭

    ©2009-2026 Movatter.jp