node.jsexpresscurlfetchubuntu-22.04

External API call timed out using fetch but working using curl


When I make a request to an external API from Node.js using fetch, I end up waiting until it times out. However, when I try the same request directly from the command line using curl, it works.

Fetch request: Data object is fine for sure

await fetch(EXTERNAL_URL, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: data,
})
.then((response) => {
        return response.json();
    })
    .then(async (data: any) => {
// treatment
}));

Curl call:

curl -X POST 'EXTERNAL_API' \      
     -H 'Content-Type: application/x-www-form-urlencoded' \
     -d 'grant_type=XXX' \
     -d 'scope=XXX' \
     -d 'redirect_uri=XXX' \
     -d 'client_id=XXX' \
     -d 'client_secret=XXX' \
     -d 'code=XXX'

I tried adding node-fetch to make the request, and I checked the configuration of the Ubuntu server (22.04), but nothing works; it still doesn't work.


Solution

  • It turned out that it was indeed a server configuration issue, specifically with the proxy. Curl was used with a proxy defined in the environment variables. So, I retrieved this proxy and tried to add it to my Node.js request.

    The code now looks like this:

    import { RequestInfo, RequestInit } from 'node-fetch';
    import { HttpsProxyAgent } from 'https-proxy-agent';
    
    const fetch = (url: RequestInfo, init?: RequestInit) => import('node-fetch').then(({ default: fetch }) => fetch(url, init));
    
    const proxyAgent = new HttpsProxyAgent(HTTPS_PROXY);
    
    [...]
    
    await fetch(EXTERNAL_URL, {
        agent: proxyAgent,
        method: 'POST',
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: data,
    })
        .then((response) => {
            return response.json();
        })
        .then(async (data: any) => {/*treatment*/});
    

    The issue was therefore due to the network on which my Ubuntu server runs, which requires going through a proxy.