Http request boundaries getting caught in pdf decoding process

I'm trying to decode a base64 pdf file and send it to another endpoint.
I used a python policy for the decoding part and here's the code

```
import base64


pdfB64 = flow.getVariable("request.content")

pdfFile = base64.b64decode(pdfB64)
flow.setVariable("pdfFileDecoded",pdfFile)
```

Now, when I send my http post request which is below

```
headers :
Accept : */*
boundary : --Boundaryy

--Boundaryy
Content-Disposition: form-data; name="testdu12janvier"; filename="testdu12janvier.pdf"

Content-Type: application/pdf

<< Heres is sensitive data which is basically a base64 encoded pdf file >>

--Boundaryy--
```

When I send this POST request and trace it in Apigee Edge, I notice that something else is encoded before the pdf file I think its either the boundary or one of the headers. This makes a corrupt pdf file which can't be read.

How do I isolate the pdf file from the request body without removing boundaries? as I'll need to send multiple in near future.

Solved Solved
0 4 605
1 ACCEPTED SOLUTION

I'm trying to decode a base64 pdf file and send it to another endpoint.
I used a python policy for the decoding part and here's the code


ok that's clear, sounds good. But... base64.b64decode() returns a bytes object. Not a String. Storing that bytes object into a flow variable will probably not ... work properly.

Now, when I send my http post request which is below


How did you construct that multi-part message? Did you use AssignMessage? AssignMessage will most likely treat everything you assign into the payload as a string. As I said above, the decoded PDF is not a string, it's a python bytes object. AssignMessage will probably try to coerce it to a string, but that won't do what you want.

How do I isolate the pdf file from the request body without removing boundaries? as I'll need to send multiple in near future.


There is a Java callout that can help produce or parse multi-part form messages . You might want to try using that.  It will work in Apigee Edge, but I think it won't work in Apigee X.

View solution in original post

4 REPLIES 4

I'm trying to decode a base64 pdf file and send it to another endpoint.
I used a python policy for the decoding part and here's the code


ok that's clear, sounds good. But... base64.b64decode() returns a bytes object. Not a String. Storing that bytes object into a flow variable will probably not ... work properly.

Now, when I send my http post request which is below


How did you construct that multi-part message? Did you use AssignMessage? AssignMessage will most likely treat everything you assign into the payload as a string. As I said above, the decoded PDF is not a string, it's a python bytes object. AssignMessage will probably try to coerce it to a string, but that won't do what you want.

How do I isolate the pdf file from the request body without removing boundaries? as I'll need to send multiple in near future.


There is a Java callout that can help produce or parse multi-part form messages . You might want to try using that.  It will work in Apigee Edge, but I think it won't work in Apigee X.

Hello @dchiesa1 

I am indeed trying to send a file not a string. The multi-part message was constructed using a javascript policy.

I was wondering if decoding the pdf and sending it as a file was possible ?


@Zyoumir wrote:

I am indeed trying to send a file not a string. The multi-part message was constructed using a javascript policy.

 


That won't work, as the JavaScript callout in Apigee specifically coerces content into String. a PDF is not a string, so it will be corrupted.  Similarly with PNG.  In Apigee, you cannot manipulate multipart/form payloads in Javascript, if any of the parts are binary.  You can do it if all of the parts are text fields, or the files in the multipart message are all plaintext files. 


@Zyoumir wrote:

I was wondering if decoding the pdf and sending it as a file was possible ?


I don't know what you mean by this.   Maybe you can elaborate.



I don't know what you mean by this.   Maybe you can elaborate.


What I mean is creating a pdf file in the python policy for example and then sending that file, I think maybe it is possible to store it in ressources.