In this article, we will see how to encode and decode base64 strings while building APIGEE proxies. As part of APIGEE, we have BasicAuthentication policy which deals with base64 encoded authorization header. But if we want to deal with any base64 encoded string other than Authorization header we should go with JavaScript policy or JavaCallout policy or PythonScript policy custom implementation. In this article, I will show you how to achieve base64 encode and decode using JavaScript policy.
Let me create a simple proxy with JavaScript policy to decode base64 encoded string. Below is the JavaScript policy configuration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | |
<Javascript async="false" continueOnError="false" enabled="true" timeLimit="200" name="JS-Base64EncodeDecode"> | |
<DisplayName>JS-Base64EncodeDecode</DisplayName> | |
<IncludeURL>jsc://Base64EncodeDecode.js</IncludeURL> | |
<ResourceURL>jsc://DecodeBase64String.js</ResourceURL> | |
</Javascript> |
In the JavaScript policy, I have included the Base64EncodeDecode js file which performs the encode and decode. Below is the JavaScript to decode base64 encoded string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var base64EncodedKey = context.getVariable("request.queryparam.key"); | |
var key = Base64.decode(base64EncodedKey); | |
print(key); |
The JavaScript which does base 64 encode and decode is available here.
The sample proxy created to demonstrate base64 encode and decode is available on GitHub. Download the sample proxy bundle and import to APIGEE Edge to play with it.
In next article we will discuss another use case. Till then Happy Coding!!!
Leave a Reply