Send PDF Files as Binary Strings
How to use the power of MuleSoft to send PDF files from Experience API to Process/System API and how to covert it back to PDF file in the second API.
Join the DZone community and get the full member experience.
Join For FreeIntroduction
In this article, I am going to explain to you how we can use the power of MuleSoft to send PDF files from Experience API to Process/System API, using the multipart/form-data
type, and to covert it back to PDF file in the second API.
Use Case
We are going to read the PDF file from the local disk using Mule’s out-of-the-box (OOTB) File connector to read in Experience API. Then we will be sending this PDF as binary with some other fields to another API (this can be named as Process API) which accepts data as multipart/form-data
. Next, we will extract this PDF binary from the received payload and convert it back to PDF and save the file to the local disk using Mule’s OOTB File connector to write.
Design Experience API
We can either create Experience API using RAML or we can create it directly in Anypoint Studio using an HTTP connector. I am going to create it directly in Anypoint Studio, as its RAML is going to be pretty straight and easy. We will be designing RAML for the Process API. Please find the Experience API as shown in the below snapshot:
Below are the file read configurations:
Working on Experience API
This API accepts 4 headers (hasattachemnt, accountNumber, content, fileName). These fields are totally hypothetical and can be anything. I have created these just to demonstrate how other fields can be passed from one API to another API along with the PDF file.
fileName is the name of the PDF file that we are going to read from our local disk and send to another API.
Now once we receive the request at Experience API, the first thing we will do is store the headers in variables so that we can use them later. Then with the help of the File read connector, we will read the PDF from the local disk. Once the PDF is read, we convert it to a binary string so that it can be sent over HTTP. Please find below the snapshot of the conversion of the PDF to binary in DataWeave:
In the next DataWeave, we will create a multipart/form-data
request so that it can be sent to process API as a payload. Please find the code snippet below:
%dw 2.0
import * from dw::core::Binaries
import dw::module::Multipart
output multipart/form-data boundary="--------------------------664925574033128632060122"
---
{
parts:
{ files:
{
headers:
{
"Content-Type" : "application/pdf"
},
content: (payload)
},
accountnumber:
{
headers:
{
},
content: vars.account
},
hasattachemnt:
{
headers:
{
},
content: vars.attachment
},
subject:
{
headers:
{
},
content: vars.content
}
}
}
Next, we call Process/System API and pass the above request as payload.
Now let's design the Process API from RAML.
Designing Process API
Please find below the RAML for Process API. Use this RAML to generate flows using Anypoint Studio.
#%RAML 1.0
title: Multipart Request Process API
description: Accept multipart/form-data as payload
types:
pdfType:
type: file
fileTypes: ['application/pdf']
/notification:
post:
description: to accept request as multipart
body:
multipart/form-data:
properties:
files:
description: attachment
type: pdfType
accountnumber:
description: account number
type: number
hasattachemnt:
description: Boolena value
type: boolean
subject:
description: Content from the experience API
type: string
responses:
200:
description: response from the API
body:
application/json:
example:
{ "status" : "received successfully"}
Once the flows are generated from the above RAML, we need to extract the PDF from the received payload and save the PDF on the local disk.
The snippet to extract the PDF is given below.
Payload:
%dw 2.0
import fromBase64 from dw::core::Binaries
output multipart/form-data
---
{
parts:
{ test:
{
headers:
{
"Content-Type" : "application/pdf"
},
content: fromBase64(payload.parts.files.content)
}
}
}
This can be your payload and can save other fields in variables separately, like accountNumber, content, etc.
Now save this payload on your local disk using MuleSoft OOTB write connector.
To extract other fields, we can use a similar syntax. For example, the accountNumber can be extracted into a variable like this:
Once you run this, you can find your copied PDF in the local directory.
That's all for this blog. Thank you.
Published at DZone with permission of Surya Veer. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments