How to Encode a String to Base64 in Postman?

To encode a string to Base64 in Postman, first write a JavaScript script, then send the request in the test script, and finally view the encoded value.

Home > Blog > How to Encode a String to Base64 in Postman?

In API development and testing, we sometimes need to encode strings using various methods, with Base64 encoding being a commonly used technique. Base64 encoding is a method that represents binary data using 64 printable characters. It is often used for transmitting data in contexts such as URLs, cookies, headers, and XML data.

In this article, we will introduce how to encode request parameter strings into Base64 format when sending requests in Postman.

Application Scenarios for Base64 Encoding

Base64 encoding has a wide range of applications in API development and testing, primarily including the following aspects:

  1. Authentication: During API authentication, it is common to Base64 encode the username and password, transmitting them as the Authorization header information.
  2. Data Transmission: In certain scenarios, sensitive data needs to be transmitted via URL or cookie. Base64 encoding can enhance the readability and security of the data.
  3. File Uploads: In file upload APIs, binary file data is typically Base64 encoded before being transmitted as part of JSON data.
  4. API Responses: Some APIs directly return Base64 encoded data, such as images or audio files. The client needs to decode this data to display or play it correctly.

It is important to note that while Base64 encoding improves data readability, it does not provide true encryption. For higher data security, encryption algorithms or other security measures should be considered.

Basic Principles of Base64 Encoding

The main purpose of Base64 encoding is to convert arbitrary binary data into a printable string containing only ASCII characters, facilitating data transmission across different systems and environments. The working principles are as follows:

  1. Character Set: Base64 encoding uses a character set containing 64 printable characters, including uppercase letters A-Z, lowercase letters a-z, digits 0-9, and two special characters "+" and "/".
  2. Encoding Process: The binary data to be encoded is divided into groups of 6 bits each, then each group is converted to the corresponding Base64 character. If the final group has fewer than 6 bits, it is padded with the "=" character.

Base64 Encoding in Postman

Step 1: Write a Script

Postman provides a JavaScript environment where you can execute custom JavaScript code in pre-request scripts or test scripts. Below is a code example of encoding a string into Base64 in Postman using JavaScript:

// String to be encoded
var stringToEncode = "Hello, World!";

// Encode the string into Base64
var encodedString = btoa(stringToEncode);

// Print the encoded string

In this code, we first define the string to be encoded (stringToEncode), then use the built-in JavaScript function btoa() to encode it into Base64. Finally, we print the encoded Base64 string.

Step 2: Test the Script

After writing the script, click "Send" to send the request and view the encoded value in the console.

Encode a String to Base64 in Postman?

In practical applications, you can read the value of stringToEncode from environment variables, encode it, and then store it back in environment variables. You can then set this Base64 encoded variable in the request parameters.If you need to decode a Base64 string, you can use the atob() function:

const base64String = "SGVsbG8sIHdvcmxkIQ==";
const originalString = atob(base64String);
console.log(originalString); // Output: "Hello, world!"
Encode a String to Base64 in Postman


Base64 encoding is an essential skill in API development and testing. It plays a crucial role in authentication, data transmission, file uploads, and API responses. The basic principle is to convert binary data into ASCII characters for transmission. In Postman, you encode data by writing JavaScript scripts and sending requests to view the encoded value.

How to Encode a String to Base64 in Postman?
To encode a string to Base64 in Postman, first write a JavaScript script, then send the request in the test script, and finally view the encoded value.