DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Convert a PDF to Text (TXT) Using Java
  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots
  • From HTTP to Kafka: A Custom Source Connector

Trending

  • How to Submit a Post to DZone
  • Spring Security JWT Validation Best Practices
  • The "Just Read the Code" Myth Is Killing Your AI Workflow
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Send PDF Files as Binary Strings

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.

By 
Surya Veer
·
Oct. 11, 21 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.1K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Introduction

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: 

Experience API

Below are the file read configurations:

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.

Headers for Experience 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:

PDF conversion: binary to 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:

JSON
 
%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.

JSON
 
#%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.

Saving the PDF on the local disk

The snippet to extract the PDF is given below.

Payload:

JSON
 
%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.

Saving payload on 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:

Output variable - accountNumber

Once you run this, you can find your copied PDF in the local directory.

Copied PDF in the local directory


That's all for this blog. Thank you.

PDF API Experience API (Tin Can API) Strings

Published at DZone with permission of Surya Veer. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert a PDF to Text (TXT) Using Java
  • Document Generation API: How to Automate Personalized Document Creation at Scale
  • Secrets in Code: Understanding Secret Detection and Its Blind Spots
  • From HTTP to Kafka: A Custom Source Connector

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook