Contact Us

January 6, 2024

June 5, 2026 12:19 am

Using basic auth with username and password in SSJS http post method

Share with

Understanding Basic Auth

Basic Auth is a simple authentication scheme that involves sending a username and password in the Authorization header of an HTTP request. The username and password are separated by a colon and the resulting string is Base64 encoded.

For example, if the username is “Sandeep” and the password is “Welcome@1”, the Authorization header would be: Authorization: Basic U2FuZGVlcDpXZWxjb21lQDE=

Implementing Basic Auth in SSJS

To use Basic Auth in an SSJS HTTP Post method, you need to create the Authorization header string and then pass it as part of the headers array in the HTTP.Post method.

ssjs-example.js

var user = 'your-username';
var pass = 'your-password';
var auth = 'Basic ' + Platform.Function.Base64Encode(user+':'+pass);
var headerNames = ['Authorization'];
var headerValues = [auth];
var requestUrl = 'https://your-request-url.com';
var contentType = 'application/json';
var jsonBody = '{"fields": {"project":{"key": "SAL"},"summary": "Creating from SFMC REST API","description": "Creating of an issue using project keys and issue type names using the REST API","issuetype": {"name": "Task"}}}';
var result = HTTP.Post(requestUrl, contentType, jsonBody, headerNames, headerValues);

The root cause of the issue is that the headers were not being passed correctly in the HTTP.Post method. The headers need to be passed as an array of names and values.

By using the correct format for the Authorization header and passing the headers as an array, you should be able to successfully authenticate your HTTP requests using Basic Auth.

Best Practices

Heads up: Make sure to handle errors and exceptions properly when making HTTP requests.

Checklist

  • Use the correct format for the Authorization header
  • Pass the headers as an array of names and values
  • Handle errors and exceptions properly
  • Use a secure connection (HTTPS) for authentication
  • Keep your username and password secure
  • Test your code thoroughly to ensure it works as expected

What is Basic Auth?

Basic Auth is a simple authentication scheme that involves sending a username and password in the Authorization header of an HTTP request.

How do I use Basic Auth in SSJS?

To use Basic Auth in SSJS, you need to create the Authorization header string and then pass it as part of the headers array in the HTTP.Post method.

What is the format for the Authorization header?

The format for the Authorization header is: Authorization: Basic <Base64 encoded string of username and password>

How do I handle errors and exceptions when making HTTP requests?

You should handle errors and exceptions properly when making HTTP requests by using try-catch blocks and logging any errors that occur.

Genetrix Technology · Salesforce Marketing Cloud Partner

Need help shipping this in production?

Genetrix builds and untangles Salesforce Marketing Cloud and Agentforce setups for teams that want it done right the first time. If anything in this post sounds familiar, talk to us before it ships.

Get in Touch with Genetrix →

Blogs for the

Business-Savvy!​

Let’s Connect

A 30 min no cost strategy session
with cloud support expert

Let’s Connect

A 30 min no cost strategy session
with cloud support expert