How-To: Convert PDF binary stream to base64 string in Apigee

Is there a sample proxy available to convert PDF binary stream to base64string in apigee?

0 10 4,245
10 REPLIES 10

Can you explain a little more as to WHY you would want to do this?

It's possible to convert binary data to base64-encoded data in Apigee Edge, either via

  • nodejs
  • Javascript callouts
  • Java callouts
  • Python callouts

But what is the purpose? Explain a little more.

We have docusign integration & using the rest api exposed by Docusign it returns the PDF document as a byte stream for a given document ID & we want to convert to base64 from the response received.

Yes, ok, you have a PDF document being returned in binary format (I suppose content-type is application/pdf , and it is an octet stream).

WHY do you wish to convert it to base64?

With nodejs it is pretty straightforward, but if you are converting it to ASCII, can we make sure you doing so for a good reason. I mean to say, Apigee Edge can of course just pass through a binary stream. The client can consume it directly.

It may be that you'll tell me "MY client cannot handle binary streams." That might be a good reason, though I cannot think of a client that would be so limited.

binarytobase64-rev2-2016-05-19.zip

I used attached proxy to test but after conversion the file shows as empty. Is there a chance of corruption during conversion?

We have some challenges in our opdk on-prem to use node.js route but js works.

To your point we are still discussing to do either in apigee edge or client but it's a quick test we are performing in apigee.

Any suggestion whats the issue with the proxy which results in black pdf?

Hi Vinay, I haven't looked at your API proxy and haven't analyzed it. I haven't done so, because I am still not clear on why you would want to convert to base64. I suppose there is a chance that the conversion is incorrect somehow. It is possible your logic is not quite right. But as I say, I have not examined it yet.

@pparekh

Agree.

As I mentioned earlier we want to do a test(more of a POC) and see the behaviour.Any one tested earlier? I am sure its possible but want to confirm.I am facing some issue with the attached proxy.

Not applicable

@vinay poreddy - I agree with @Dino and have the same question about the need to do the conversion. Assuming that you are not going to change anything that is in the actual PDF payload data, then why go through the added 'cost' of doing the conversion. Additionally, if you don't need to do the conversion, then you may be able to take advantage of Apigee Edge's streaming capability.

here is a sample proxy that converts a binary stream to base64.

https://github.com/DinoChiesa/ApigeeEdge-Java-Simple-Base64

Thanks Dino.