4

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.

ApiRhys
  • 43
  • 1
  • 3

5 Answers5

3

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.

Community
  • 1
  • 1
brandonscript
  • 68,675
  • 32
  • 163
  • 220
1

The capability to simply inject a HTTP Basic Auth header, or to generate the base64 encoding of a string, is not built in, currently.

You can do it with a Javascript callout, like this.

policy XML:

<Javascript name='JavaScript-SetAuthz'>
  <ResourceURL>jsc://nameOfTheJavaScriptFileHere.js</ResourceURL>
</Javascript>

and the JavaScript

Cheeso
  • 189,189
  • 101
  • 473
  • 713
1

We also added a sample that shows how to do this in JavaScript:

https://github.com/apigee/api-platform-samples/tree/master/sample-proxies/base64encoder

ap-andrew
  • 131
  • 4
  • This works. When working to incorporate it into an existing Api-Proxy manually, I had to upload the scripts `enc-utf16-min.js, enc-base64-min.js, core-min.js` by attaching each one to a javascript policy that I later deleted ... but not before adding the `` tags in my actual javascript policy that needed to reference them like so: https://github.com/apigee/api-platform-samples/blob/master/sample-proxies/base64encoder/apiproxy/policies/EncodeAuthHeader.xml – pulkitsinghal Apr 17 '14 at 01:16
1

There is now newer functionality built into Apigee -- encodeBase64 and decodeBase64 built into Message templates. For more details see: https://docs.apigee.com/api-platform/reference/message-template-intro#base64-encoding-functions

Michael Lucas
  • 733
  • 1
  • 8
  • 23
0

-- Lack the reputation to comment (such shame) so adding an answer --

Since you asked specifically about encoding text and not binary content, you can make a minor improvement to Cheeso's answer by using b64encode instead of encodestring:

base64string = base64.b64encode('%s:%s' % (username, password))

This is also a tiny bit quicker in my testing.