Convert PDF stream to base64 string in APIGEE

Not applicable

Hi @Dino,

I am doing a multipart form-data POST to upload a pdf file to Amazon S3. We have a requirement to virus scan the PDF before sending to S3. There is a virus scan API which does the job but the pre-requisite to call the API is that the payload should be Base64 encoded.

Therefore, I need to convert the PDF file stream to Base64 encoded string in APIGEE.

I have seen a similar post with no solution in that (convert pdf stream to base64)

Would you be able to help me achieving this?

0 4 4,188
4 REPLIES 4

Hi Piyush

Yes, you can base64 encode an octet stream in Apigee Edge.

If I were doing this, I would want to do it in Python callout, or a Java callout, or a JS callout.

Here is a suitable Java callout that does this.

The configuration would look like this:

<JavaCallout name='Java-Base64Encode'>
  <Properties>
    <Property name='action'>encode</Property>
    <Property name='string-output'>true</Property>
  </Properties>
  <ClassName>com.google.apigee.edgecallouts.Base64</ClassName>
  <ResourceURL>java://edge-custom-base64-1.0.2.jar</ResourceURL>
</JavaCallout>

This will slurp in the request or response .content (depending on where you attach the policy) , and place an encoded form of it in "b64_result".

You can then use ServiceCallout to send that as a payload to the Virus scanner. And if that succeeds, then send the original request.content into S3.

@dino : Can you let us know how do you post message for decode to work? the sample which is given works via online but wondering how to test the proxy for decode to work via apigee? It throws error - not Base64

Not applicable

No, you won't be able to use that. The answer is as I provided 2 years ago. We don't need any other answers here. The original answer is still the right answer.