How to read pdf attachment in the request,do Base64encode and add it as xml tag to the request

Hi Everyone,

I want to create a API Proxy which will take XML Request with pdf as an attachment. I have to take that pdf attachment and convert it to Base64 Encoded value and then insert that encoded value as one new xml element to the original XML request from the client and send it to the Target.

I am using on-premise Apigee Edge.

Please suggest me with sample code.

Ravi

0 2 265
2 REPLIES 2

It's not clear what you mean XML Request with PDF as an attachment. The payload is either XML or Binary (PDF) - it cannot be both.

Anyway, while you sort that out, base64 encoding is simple in Apigee. If you're dealing with purely strings, you can use a Message Template to do this.

BUT, it sounds like you're working with binary files, like a PDF. In this case, you need to consider how to handle the file stream and/or byte arrays. You will need to use a Java callout policy to handle it properly. Luckily, there is a prebuilt sample that does exactly this. You can take the sample and upload the included proxy bundle to your Apigee Private Cloud and test immediately. Just issue a cURL from the command line like so -

curl -L -X POST 'https://MY_APIGEE_HOSTNAME/base64-encoder/t2?action=encode' \
-H 'Content-Type: application/pdf' \
--data-binary '@sample.pdf'

The response will be a Base64 encoded string representation of the PDF life. Viola!

There is a Java callout that parses MTOM/XOP.   But it is not able to CREATE an MTOM/XOP message.

There is a separate Java callout that parses or constructs Multipart forms.  

One of these might be helpful?