Apigee Base64 decoding to pdf

Hello Apigee comunity, im trying to return a pdf as response on an API. 
I get a base64 string from the bakend, I decode it and i send it as response content. 
The problem I'm having is that the returned pdf is blank.
I've tried a python script to decode it: 

 

 

import base64

def decode (encoded):
  decoded = base64.b64decode(encoded)
  return decoded

encoded = flow.getVariable("your-reference-variable-here")

decoded = decode(encoded)

 

 

I've tried:

{decodeBase64(encoded_value)} 

on an Assign Message also, but it always returns a blank pdf.

This is the string I get from the backend:

martins20_0-1664201665161.png


Do you have an idea on what the problem can be?

Thank you
@dchiesa1 

 

Solved Solved
0 4 321
1 ACCEPTED SOLUTION

I get a base64 string from the bakend, I decode it and i send it as response content.
The problem I'm having is that the returned pdf is blank

the problem is that decoding with the message template will try to coerce the result into a text string.  PDF is not a text string, it includes bytecodes that cannot be encoded into a string. 

So you will need to decode and then keep the result as a stream of bytes. You can do that in Java, by using the response.setContent() method that accepts an InputStream. 

In fact there is an existing Java callout that does this for you.  Find it here.  Try and see if it works for you. 

View solution in original post

4 REPLIES 4

I get a base64 string from the bakend, I decode it and i send it as response content.
The problem I'm having is that the returned pdf is blank

the problem is that decoding with the message template will try to coerce the result into a text string.  PDF is not a text string, it includes bytecodes that cannot be encoded into a string. 

So you will need to decode and then keep the result as a stream of bytes. You can do that in Java, by using the response.setContent() method that accepts an InputStream. 

In fact there is an existing Java callout that does this for you.  Find it here.  Try and see if it works for you. 

Hello @dchiesa1 ,

I have a very similar problem, but the callout you said to use doesn't work for my case, it says :

Error Deploying Revision 26 to dev
Unexpected Validation Error MALFORMED. 

Do you have an idea on why that might happen ?

Can you give me some additional information please?

When / where do you see the "MALFORMED" message?  Can you provide a screenshot?

Also what version of Apigee are you using?

What exactly are you deploying?  Did you deploy the example bundle that I included in the repo?  I am guessing that "Revision 26" is not the sample bundle, but is maybe your own API proxy bundle.  To eliminate that variable as a possible cause of the problem, can you try deploying the sample bundle that I provided, with no changes, and tell me what your results are? 

 

@dchiesa1 Yes, it works (!!!) thank you!