I am using the Fetch API introduced in NodeJS 18 to access an API. However, the request always times out after 10 seconds with the following error message.
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
cause: ConnectTimeoutError: Connect Timeout Error
at onConnectTimeout (node:internal/deps/undici/undici:8522:28)
at node:internal/deps/undici/undici:8480:50
at Immediate._onImmediate (node:internal/deps/undici/undici:8509:37)
at processImmediate (node:internal/timers:478:21) {
code: 'UND_ERR_CONNECT_TIMEOUT'
}
}
I want to INCREASE the time before the fetch request times out, however I have not been able to find way to do this with the built-in NodeJS API.
As of Node 20 (>= 17.3.0 actually), you can use an AbortSignal
without the need to install undici
:
const response = await fetch(process.env.CORREOS_URLENVIOS, {
method: 'POST',
body: soap,
headers: {
'Authorization': 'Basic ' + Buffer.from(process.env.CORREOS_USERNAME + ':' + process.env.CORREOS_PASSWORD).toString('base64'),
'Content-Type': 'application/soap+xml; charset=utf-8',
'Accept': '*/*'
},
signal: AbortSignal.timeout(60 * 1000), // <=== HERE
});