How to emulate a curl request using node-fetch module How to emulate a curl request using node-fetch module curl curl

How to emulate a curl request using node-fetch module


Authorization header is wrong.

-u "client_id:secret"

says that curl is using a Basic Authentication.

You should add authorization header

Authorization: Basic <base64 encoded "client_id:secret">


Solution using yours as base, since I struggled a few minutes to have it working.

// get the client_id and secret from https://developer.paypal.com/developer/applications/const clientIdAndSecret = <client_id:secret>const base64 = Buffer.from(clientIdAndSecret).toString('base64')fetch('https://api.sandbox.paypal.com/v1/oauth2/token', {      method: 'POST',      headers: {        'Content-Type': 'application/x-www-form-urlencoded',        'Accept-Language': 'en_US',        'Accept': 'application/json',        'Authorization': `Basic ${base64}`,      },      body: 'grant_type=client_credentials'})