Skip to content

Passing a custom agent to fetch() #1489

@mataha

Description

@mataha

This would solve...

In some otherwise trivial cases for fetch() a custom, one-purpose agent is required to manage a request, e.g. in order to check a server's identity manually or to disregard that verification completely (self-signed certificates come to mind). Current approach, from what I've seen, requires passing a set of TLS options to a dispatcher explicitly, then using said dispatcher to invoke a request - maybe it's just me, but I feel this could be simplified.

The implementation should look like...

Using https.Agent as an example:

import https from 'node:https';

const result = await fetch("https://example.com", {
    (...)
    agent: new https.Agent({
        rejectUnauthorized: false,
    }),
});

(...)

I have also considered...

Some parameters can be set via alternative means; environment variables come to mind:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
process.env.NODE_EXTRA_CA_CERTS = ...;

However, I don't think applying this to every request is a good idea (same goes for any global agents).

Additional context

Implementations in similar projects:

Possibly relevant issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions