javascript - How to emulate a curl request using node-fetch module - Stack Overflow

i have an e-merce application and trying to reach out to the paypal rest api, "paypal for partners

i have an e-merce application and trying to reach out to the paypal rest api, "paypal for partners" service specifically, i did read the Paypal Documentation and its all good but the problem is that they mentioned the request example using curl like this :

curl -v  \
   -H "Accept: application/json" \
   -H "Accept-Language: en_US" \
   -u "client_id:secret" \
   -d "grant_type=client_credentials"

Or

using postman with basic Auth:

  • Username: Your client ID.

  • Password: Your secret.

iam trying to implement the same thing but using node-fetch from node.js

const fetch = require('node-fetch');

function authenticatePaypal() {
    fetch('', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
            'client_id': 'secret'
        },
        body: {
            "grant_type": "client_credentials"
        }
    }).then(reply => {
        console.log('success');
        console.log(reply);
    }).catch(err => {
        console.log('error');
        console.log(err);
    });
}

module.exports = {
    authenticatePaypal: authenticatePaypal
};

and i get this response of 401 Unauthorized:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url: '',
     status: 401,
     statusText: 'Unauthorized',
     headers: Headers { [Symbol(map)]: [Object] } } }

i tried post man and it worked in postman, i know that there is something wrong in my node-fetch implementation, this is my first time dealing with basic Auth in json format.

i have an e-merce application and trying to reach out to the paypal rest api, "paypal for partners" service specifically, i did read the Paypal Documentation and its all good but the problem is that they mentioned the request example using curl like this :

curl -v https://api.sandbox.paypal./v1/oauth2/token \
   -H "Accept: application/json" \
   -H "Accept-Language: en_US" \
   -u "client_id:secret" \
   -d "grant_type=client_credentials"

Or

using postman with basic Auth:

  • Username: Your client ID.

  • Password: Your secret.

iam trying to implement the same thing but using node-fetch from node.js

const fetch = require('node-fetch');

function authenticatePaypal() {
    fetch('https://api.sandbox.paypal./v1/oauth2/token', {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json',
            'Accept-Language': 'en_US',
            'client_id': 'secret'
        },
        body: {
            "grant_type": "client_credentials"
        }
    }).then(reply => {
        console.log('success');
        console.log(reply);
    }).catch(err => {
        console.log('error');
        console.log(err);
    });
}

module.exports = {
    authenticatePaypal: authenticatePaypal
};

and i get this response of 401 Unauthorized:

Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]:
   { body:
      PassThrough {
        _readableState: [ReadableState],
        readable: true,
        _events: [Object],
        _eventsCount: 2,
        _maxListeners: undefined,
        _writableState: [WritableState],
        writable: false,
        allowHalfOpen: true,
        _transformState: [Object] },
     disturbed: false,
     error: null },
  [Symbol(Response internals)]:
   { url: 'https://api.sandbox.paypal./v1/oauth2/token',
     status: 401,
     statusText: 'Unauthorized',
     headers: Headers { [Symbol(map)]: [Object] } } }

i tried post man and it worked in postman, i know that there is something wrong in my node-fetch implementation, this is my first time dealing with basic Auth in json format.

Share Improve this question asked Jan 29, 2019 at 12:15 Mahmoud FawzyMahmoud Fawzy 1,67513 silver badges22 bronze badges 15
  • i'm sure that the problem is in my headers object i don't know how to implement the basic Auth in json format – Mahmoud Fawzy Commented Jan 29, 2019 at 12:18
  • Try removing this header 'Content-Type': 'application/json, I am not really sure, but try and confirm if that maybe the cause – elraphty Commented Jan 29, 2019 at 12:21
  • 1 @MahmoudFawzy client id seems like a basic authentication so it should be something like Authorization: Basic <secret in base64> – karoluS Commented Jan 29, 2019 at 12:41
  • 1 @MahmoudFawzy it should be Authorization: Basic <client_id:secret in base64> – karoluS Commented Jan 29, 2019 at 12:49
  • 1 @MahmoudFawzy are you sure you are suppouse to send it as a JSON? I think it should be just a string body: 'grant_type=client_credentials' – karoluS Commented Jan 29, 2019 at 13:15
 |  Show 10 more ments

2 Answers 2

Reset to default 3

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./developer/applications/
const clientIdAndSecret = <client_id:secret>
const base64 = Buffer.from(clientIdAndSecret).toString('base64')

fetch('https://api.sandbox.paypal./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'
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744299334a4567420.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信