Call multipart/form-data service

 

To call a multipart/form-data service we will first deploy the sample service which we created in earlier tutorial – Create multipart/form-data service

Upload the below zip file to Anypoint runtime manager or cloudhub and we will call the service through form upload html

Create multipart/form-data service jar – form-upload-sample-jar

Form upload html – form_upload

Form upload zip – form_upload

 

Endpoint to call – http://form-upload-sample.us-e2.cloudhub.io/upload (please change the same in form-upload.html)

We can see the different keys and their values in logs if we call the service through form upload html

 

Call multipart/form-data service

 

Now lets create a mule application which will call the above multipart/form-data service

Call multipart/form-data service

 

Main logic for calling multipart/form-data service goes in writing a correct transformation

Dataweave already support the multipart conversion

More details – https://docs.mulesoft.com/mule-runtime/4.2/dw-multipart

For calling our services we have to add below transformation

%dw 2.0
import dw::module::Multipart
output multipart/form-data
var myFileText = "File Text"
---
{
	parts: {
		checkbox: Multipart::field("checkbox", "Off"),
		password: Multipart::field("password", "mysecret"),
		text: Multipart::field("text", "myText"),
		myfile: Multipart::field("myfile", myFileText, "text/plain", "myFile.txt")
	}
}

We have only two type of form elements – Text and File so we have to understand how we can send them.

Sending Text e.g. checkbox, password, text

Multipart::field(“checkbox”, “Off”)

First argument is the element name and second is the value.

Element name is quite import and should be in sync with the name in form html or RAML

More on fields – https://docs.mulesoft.com/mule-runtime/4.2/dw-multipart-functions-field

Sending File e.g. myfile

Multipart::field(“myfile”, myFileText, “text/plain”, “myFile.txt”)

First argument is the element name – Element name is quite import and should be in sync with the name in form html or RAML

Second is file content – this can be variable or any resource available in src/main/resources

Third – content type of file

Fourth is file name

More on file – https://docs.mulesoft.com/mule-runtime/4.2/dw-multipart-functions-file

 

Element name is quite import and should be in sync with the name in form html or RAML

<html>
<body>
<p>Different type of input types:</p>
<form action="http://form-upload-sample.us-e2.cloudhub.io/upload" method="post" enctype="multipart/form-data">
	<label for="fname">checkbox: </label><input type="checkbox" name="checkbox"><br>
	<label for="fname">password: </label><input type="password" name="password"><br><br>
	<label for="fname">text: </label><input type="text" name="text"><br><br>
	<label for="fname">file: </label><input type="file" id="myfile" name="myfile"><br><br>
	<label for="fname">submit: </label><input type="submit" name="fileContent"><br><br>
</form>
</body>
</html>

 

So after the transformation if we analyse the payload – it will look more like

payload.parts write “application/json”

{
	"checkbox":{
		"headers":{
			"Content-Disposition":{
				"name":"checkbox",
				"subtype":"form-data"
			}
		},
		"content":"Off"
	},
	"password":{
		"headers":{
			"Content-Disposition":{
				"name":"password",
				"subtype":"form-data"
			}
		},
		"content":"mysecret"
	},
	"text":{
		"headers":{
			"Content-Disposition":{
				"name":"text",
				"subtype":"form-data"
			}
		},
		"content":"myText"
	},
	"myfile":{
		"headers":{
			"Content-Type":"text/plain",
			"Content-Disposition":{
				"name":"myfile",
				"filename":"myFile.txt",
				"subtype":"form-data"
			}
		},
		"content":"File Text"
	}
}

 

This is similar to what multi-part api expect

Add a requester

Call multipart/form-data service

 

Add logger to check the response

 

Now run and test the service

http://localhost:8081/call-form-upload

 

MuleSoft Cloudhub logs

 

11:43:11.148 04/24/2020 Worker-0 [MuleRuntime].cpuLight.01: [form-upload-sample].form-upload-sampleFlow.CPU_LITE @be5ddf1 INFO
event:a6709f80-85f2-11ea-93e4-9aaf65ed66d8 "password value - mysecret"
11:43:11.150 04/24/2020 Worker-0 [MuleRuntime].cpuLight.04: [form-upload-sample].form-upload-sampleFlow.CPU_LITE @be5ddf1: [form-upload-sample].form-upload-sampleFlow.CPU_LITE @be5ddf1 INFO
event:a6709f80-85f2-11ea-93e4-9aaf65ed66d8 "text value - myText"
11:43:11.151 04/24/2020 Worker-0 [MuleRuntime].cpuLight.03: [form-upload-sample].form-upload-sampleFlow.CPU_LITE @be5ddf1 INFO
event:a6709f80-85f2-11ea-93e4-9aaf65ed66d8 "checkbox value - Off"
11:43:11.728 04/24/2020 Worker-0 [MuleRuntime].cpuLight.02: [form-upload-sample].form-upload-sampleFlow.CPU_LITE @be5ddf1 INFO
event:a6709f80-85f2-11ea-93e4-9aaf65ed66d8 "file name - myFile.txt"
11:43:11.905 04/24/2020 Worker-0 [MuleRuntime].cpuLight.02: [form-upload-sample].form-upload-sampleFlow.CPU_LITE @be5ddf1 INFO
event:a6709f80-85f2-11ea-93e4-9aaf65ed66d8 "content type - text/plain"
11:43:11.946 04/24/2020 Worker-0 [MuleRuntime].cpuLight.02: [form-upload-sample].form-upload-sampleFlow.CPU_LITE @be5ddf1 INFO
event:a6709f80-85f2-11ea-93e4-9aaf65ed66d8 file content File Text

 

Sample Application – call-form-upload-sample

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments