KB Article #180149

How to create content-body payload of type "multipart/form-data"

Problem

How to create content-body payload of type "multipart/form-data"

Resolution

There's no out of the box filter which allows you to create multipart/form-data message. You can use a scripting language filter to do it, by using com.vordel.mime.Multipart class.

Here is a short sample of the script that can be used (Groovy):



import com.vordel.mime.Multipart;
import com.vordel.mime.HeaderSet;
import java.util.UUID;
import com.vordel.mime.ContentType;
import com.vordel.mime.Body;
import com.vordel.dwe.InputStreamContentSource;

def invoke(msg)
{
//create the multipart message
def outerContType = new ContentType(ContentType.Authority.MIME, "multipart/form-data");
outerContType.put("boundary", UUID.randomUUID().toString());
def multipart = new Multipart(outerContType);


//here you should create some Body to fill the parts of the message

def content = "message"
InputStream inputStream = new ByteArrayInputStream(content.getBytes());

//handle headers
def contType = new ContentType(ContentType.Authority.MIME, "application/xml");
contType.put("type", "application/xml");
HeaderSet headers = new HeaderSet();
headers.setHeader("Content-Type", contType);
headers.setHeader("Content-Disposition", "form-data; name=\"aName\"; filename=\"aFileName\"");

//handle body
def rawBody = Body.create(headers,contType, new InputStreamContentSource(inputStream));

//add the part to the message
multipart.addPart(rawBody);


//set the message in content.body attribute
msg.put("content.body", multipart);
return true;
}