Search code examples
node.jsnode-libcurl

Curl with digest auth


I have one endpoint, I have been testing with comand ubuntu, the endpoint require username and password, I run this:

curl --digest -u username:pass --location --request POST 'http://url' \
--header 'Content-Type: application/json'

so I just want run in node js usign node-libcurl, so I don't found the correct way to add digest authorization(username:pass), this code:

const { curly } = require('node-libcurl')

exports.test = async () => {

var user = 'user';
var pass = '45213';

var auth = new Buffer.alloc(user + ':' + pass).toString('base64');

const { data } = await curly.post('http://url', {
    postFields: JSON.stringify({ field: 'value' }),
    httpHeader: [
      'Content-Type: application/json',
      'Accept: application/json',
      `Authorization: Basic  + ${auth}`
    ],
})
      
      console.log(data);
};

using the node-libcurl library, is there a way to add authentication validation??... using username and password

I'm receiving this error:

TypeError [ERR_INVALID_ARG_TYPE]: The "size" argument must be of type number.


Solution

  • Using this code

    const auth = new Buffer.alloc((user + ':' + pass).length).toString('base64');