What's the easiest way to do Base64 encoding within an Apigee policy? I need to encode the text of an SMS to be sent programmatically.
I know I could include the code explicitly, but I'd much prefer to use a capability that is built in if available.
The most efficient way to accomplish this is to create a python script policy, then use python's built-in base64 module to build a simple function that base64-encodes or -decodes an argument.
If you snag the sample proxies from here: http://apigee.com/docs/api-services/content/api-proxy-samples you'll find a python example in /simpleProxy/apiproxy/resources/py
.
The policy XML is like this:
<Script name="Script-GenerateAuthHeader">
<ResourceURL>py://Authheader.py</ResourceURL>
</Script>
The python code would look like this:
import base64
username = flow.getVariable("request.formparam.client_id")
password = flow.getVariable("request.formparam.client_secret")
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
flow.setVariable("my.base64.blob", base64string)
Alternatively, if python isn't your style, you could do this in javascript using a js resource, or even directly in a Node.js proxy.