Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlFetch equivalent of CURL -u "<username>:<password>"

I have experience making CURL calls in GAS using headers and payload, but I have never done a CURL command using the -u option before. According to the API spec, I must use the -u option. I just don't know how to convert that to GAS. Here is my code so far:

function updateStatus()
{ 
  //Build header.
  var header =
  {
      'Content-Type': 'application/json', //Set content type to JSON.
  };

  //Put it all together.
  var options =
  {
      'method'     : 'get',    
      'headers'    : header     
  };

  //Make Login call to When I work.
  var responseGetPlan = UrlFetchApp.fetch('my url', options);
  var strResponseGetPlan = responseGetPlan.getContentText();
  Logger.log('Get Plan Response: ' + strResponseGetPlan); //Log response.

  var parsedData = JSON.parse(strResponseGetPlan); //Parse into JSON format.
  var strId = parsedData.id;  
  Logger.log(strId);
}
like image 400
m7hacke Avatar asked Dec 30 '25 03:12

m7hacke


1 Answers

curl -u uses Basic authentication, which is a simple base64 encoding of a concatenated "username:password" string. You would send the following as headers.

Authorization: 'Basic ' + Utilities.base64Encode('username:password')

References:

  • RFC7617
  • curl Basic Authentication
  • Utilities
like image 149
TheMaster Avatar answered Jan 01 '26 22:01

TheMaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!