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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

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

Related

  • How To Convert HTML to PNG in Java
  • MDC Logging With MuleSoft Runtime 4.4
  • MuleSoft Integration With RabbitMQ
  • Understanding CIDRs and Public vs Private IPs

Trending

  • DGS GraphQL and Spring Boot
  • Building Resilient Identity Systems: Lessons from Securing Billions of Authentication Requests
  • The Role of AI in Identity and Access Management for Organizations
  • Monolith: The Good, The Bad and The Ugly
  1. DZone
  2. Coding
  3. Languages
  4. Transforming multipart/form-data Into XML Using MuleSoft and JavaScript

Transforming multipart/form-data Into XML Using MuleSoft and JavaScript

Curious about how to take a data set and transform it into XML format? We discuss how to do so in MuleSoft using JavaScript.

By 
Rajesh ale user avatar
Rajesh ale
·
Mar. 07, 18 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
10.2K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, I'll discuss how to transform multipart/form-data in MuleSoft using JavaScript.

I have been going through MuleSoft for a while and haven't seen any docs or references on how to parse non-XML or multipart data. I believe this article will give some basic information on it.

I tried to solve this using MuleSoft and JavaScript. Below is the sample flow that I used.

  1. Create a MuleProject and add an HTTP connector (set HTTP_Listner_Configuration).

  2. Make sure to unselect the Parse Request in the advanced properties of the HTTP connector (this will make the multipart/form-data payload into a byte array).

  3. Connect ByteArraytoString to the HTTP connector.

  4. Use logger to see how the input payload is getting converted from byte to String format in the debugger.

  5. Connect JavaScript and use the below code to transform the string to an XML format.

Remember, while setting up the HTTP connector config in the Basic Settings path, use "/multipart" and allowed methods to "POST." This will allow us to send POST requests for the URI /multipart (or you can use your own custom URI).

Image title

In the below JavaScript, I did the following few things:

  1. First, I read the payload using log.info.

  2. And if I am able to read it, I then convert it to string format and, to that string, I apply a few string operations.

  3. The variables documentValue, requestIdValue, echoBack, and KeepALive will contain the values that are coming from the input after the string operations are run.

  4. Build an XML file using the variable resp and print the response in the console.

log.info(payload);
if(payload){
var str = payload.toString();
print("Input Payload is:" +payload);
var docstart = str.indexOf('name="document"') + 16;
var documentValue = str.substr(docstart, str.indexOf('---', docstart)
 - docstart);
    documentValue=documentValue.trim();
var idstart = str.indexOf('name="requestId"') + 16;
var requestIdValue = str.substr(idstart, str.indexOf('---', idstart)
 - idstart);
requestIdValue=requestIdValue.trim();
var echoBack = str.indexOf('name="echoBack"') + 16;
var echoBackValue = str.substr(echoBack, str.indexOf('---', echoBack)
 - echoBack);
echoBackValue=echoBackValue.trim();
var keepAlive = str.indexOf('name="keepAlive"') + 16;
var keepAliveValue = str.substr(keepAlive, str.indexOf('---', keepAlive)
 - keepAlive);
keepAliveValue=keepAliveValue.trim();
var documentId = str.indexOf('name="thedocumentID"') + 21;
var documentIdValue = str.substr(documentId, str.indexOf('---', documentId)
 - documentId);
documentIdValue=documentIdValue.trim();


        var resp = '<request><document>';
resp = resp.concat(documentValue);
resp = resp.concat('</document><requestID>');
resp = resp.concat(requestIdValue);
resp = resp.concat('</requestID><echoBack>');
resp = resp.concat(echoBackValue);
resp = resp.concat('</echoBack><keepAlive>');
resp = resp.concat(keepAliveValue);
        resp = resp.concat('</keepAlive><thedocumentID>')
        resp = resp.concat(documentIdValue);
        resp = resp.concat('</thedocumentID></request>');
        message.setPayload(resp);
        result=message;
     print("Output Payload is:" +resp);
}

The below image depicts how to send an HTTP Request with multipart/form-data and get the response back as an XML file using SoapUI:

For sending a request in SoapUI:

  1. Select Media Type as multipart/form-data and Method as POST.

  2. Make sure the resource is the URI path, /multipart.

  3. As Mule Runtime is on my localhost, the endpoint is http://localhost:8081.

  4. Once you hit the green button, it will trigger a request to our HTTP connector in Mule Runtime and parse it using our JavaScript and send the response back to SoapUI (which is an XML payload).

Image title

Thank you!

XML JavaScript MuleSoft

Opinions expressed by DZone contributors are their own.

Related

  • How To Convert HTML to PNG in Java
  • MDC Logging With MuleSoft Runtime 4.4
  • MuleSoft Integration With RabbitMQ
  • Understanding CIDRs and Public vs Private IPs

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!