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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Convert a PDF to Text (TXT) Using Java
  • Scholcast: Generating Academic Paper Summaries With AI-Driven Audio
  • How to Get Plain Text From Common Documents in Java
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 6)

Trending

  • Scaling in Practice: Caching and Rate-Limiting With Redis and Next.js
  • Start Coding With Google Cloud Workstations
  • Simplify Authorization in Ruby on Rails With the Power of Pundit Gem
  • Scaling InfluxDB for High-Volume Reporting With Continuous Queries (CQs)
  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 user avatar
Surya Veer
·
Oct. 11, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
16.6K Views

Join the DZone community and get the full member experience.

Join For Free

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
  • Scholcast: Generating Academic Paper Summaries With AI-Driven Audio
  • How to Get Plain Text From Common Documents in Java
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 6)

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!