How to Check if a Text String is Base 64 Encoded in Node.js
If we know plain text strings are Base64 encoded, we know that content can travel over text-based mediums.
Using the below Node.js code, we can take advantage of a free API that rapidly checks text strings for base64 content. This can act as a quick & secure validation step before moving text data from one location to another.
We can structure our API call in two quick steps. That starts with installing the SDK; we can do so either by running the following command:
npm install cloudmersive-convert-api-client --save
Or by adding the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Finally, we can copy the ready-to-run code below into our file and authorize our request with a free-tier API key (these allow up to 800 API calls per month & can be obtained by registering a free account on the Cloudmersive website):
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.EditTextApi();
var request = new CloudmersiveConvertApiClient.Base64DetectRequest(); // Base64DetectRequest | Input request
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editTextBase64Detect(request, callback);
Our response will provide a Boolean declaring if the text string is in Base64. That’s all there is to it!