Base64 Decode any string in Apigee

Hi,

Is there any policy in Apigee which can decode base 64 string ? I checked Basic Authentication policy but seems it decodes only username and password. But this encoded string has some custom data. Only way i can think of is to use java script to decode this string. Is there any other way ?

Solved Solved
0 11 3,750
1 ACCEPTED SOLUTION

@nikhilchawla

Yes - JS is your best bet.

Keep in mind that not all base64 strings decode into strings. In general decoding a base64-encoded value will result in an array of bytes, which might represent a string, but might not.

View solution in original post

11 REPLIES 11

@nikhilchawla

Yes - JS is your best bet.

Keep in mind that not all base64 strings decode into strings. In general decoding a base64-encoded value will result in an array of bytes, which might represent a string, but might not.

ps: we hope to add some handy features for doing this in the future, in the context of a MessageTemplate. Ref tickets APIRT-3489 and APIRT-1180 .

@Dino do we have any details about APIRT-3489 and APIRT-1180 or any link to understand base64 to String support and viceversa support as indicated or any detailed link will help. I tried searching with the Ref Tickets but could not get indicatred information

the changes for APIRT-3489 (now known as b/67168078) will be deployed to production next week! Check the cloud release notes (not yet posted) for information on "Message Template" enhancements.

APIRT-1180 is still pending. But you probably don't need that.

@Dino would like to know, when you indicate about "Message Template" enhancements means conversion from base64 to String and viceversa ? using a new Policy (Mediation based Policy)? just want to make sure I get it correct which may be available later post week for apigee Cloud env ..

The Message Template will be enhanced with a new set of functions. one pair of functions will be a base64Encode and base64Decode. These functions will be usable within any Message template. The message template is not a new policy, but is the thing you specify in AssignMessage / Set / Payload, and in various other places within Apigee Edge policies.

Where-ever there is a message template, these functions will be available.

Full details in the official documentation, it will arrive next week.

Thanks @Dino this helps

Not applicable

You can use this code snippets for encoding and decoding

Encoding Base64 String

'use strict';
let data = 'stackabuse.com'; 
let buff = new Buffer(data); 
let base64data = buff.toString('base64');
console.log('"' + data + '" converted to Base64 is "' + base64data + '"'); 

OUTPUT

"stackabuse.com" converted to Base64 is "c3RhY2thYnVzZS5jb20="

Decode Base64 String

'use strict';
let data = 'c3RhY2thYnVzZS5jb20='; 
let buff = new Buffer(data, 'base64'); 
let text = buff.toString('ascii');
console.log('"' + data + '" converted to Base64 to ASCII is "' + text + '"');

OUTPUT

"c3RhY2thYnVzZS5jb20=" converted to Base64 to ASCII is "stackabuse.com"

Buffer is not defined error

yes, you cannot use that code in a JavaScript callout.

Thanks All. I was able to implement my requirement with JS. But i would definitely like to see some out of box policy from Apigee to do this.