Tag Archives: MIME

webMethods 8.2 How to POST a file to a HTML form

This was a lot harder than I first thought and the only way I was able to do it was by making the MIME document by hand. I first tried using the pub.mime:createMimeData function together with pub.mime:addBodyPart and finally creating the document using pub.mime:getEnvelopeStream and sending it with pub.client:http, but my HTML test form never recognized the data it received. After a lot of searching Google and the WmUsers forum I found a solution and I’m going to share that solution here.

My test HTML form:

<form method="post" enctype="multipart/form-data" action="index.php">

File:<input name="File" id="File" type="file" /><br />

User:<input name="UserID" id="UserID" type="text" value=""/><br />

Password:<input name="Password" id="Password" type="password" value="" /><br />

<input type="submit" value="Send">


The function of the form is that it will accept a file for upload if the user credentials are correct

The index.php file looks like this:

file_put_contents("recievefile.log", date(DATE_RFC2822) . "\n" . print_r($_FILES, true), FILE_APPEND);
file_put_contents("recievefile.log", date(DATE_RFC2822) . "\n" . print_r($_POST, true), FILE_APPEND);

This is just for debugging. The php file writes the contents of the global FILE and POST variables to a logfile where I can see the result. In a production environment you would point the form data to a regular input script (php, .NET ASP, JAVA and whatnot)

So how do we POST to this form from webMethods? The solution is to manually creating the MIME document. So here is what I did to fill the form variables from webMethods:
My MIME document (a simple string in webMethods):

Content-Disposition: form-data; name="File"; filename="filename.xml"; 
Content-Type: text/xml 


Content-Disposition: form-data; name="UserID";
Content-Type: text/plain


Content-Disposition: form-data; name="Password";
Content-Type: text/plain



Lets go through the different lines. The “—-=Part–123456789” line is a part separator. It can be almost anything as long as it starts with “- -“. A part is simply one variable definition to send to the form. We have three form variables to fill so we have three part sections.
The “Content-Disposition” maps the section to the form variable. First we say that it is form-data we are sending (same as form is declaring in enctype). The name key value pair maps agains the forms key value pairs (File to the first input, UserID to the second and so on..)
The “Content-Type” simply says what type of data we are sending. For a simple UserID variable text/plain is sufficient. For the xml file I’m using the text/xml content type.
In the “variableWithThe?” section we simply paste the content for the form variables (File, UserID and Password). This is pasted as plaintext string in my case since I do not use any binary data (images, zip files and so on).

Now, to get the pub.client:http service to correctly send this to the form we need to do one more thing. The default header for pub.client:http needs to be corrected so we set one header row to the following:
Key: content-type
Value: multipart/form-data;boundary=”–=Part–123456789″
The “method” variable is set to “POST” and the “url” to the url of the form

After this you should be ready to send xml files to the HTML form and it should recognize the data as a file (called “File”) and two post variables (“UserID” and “Password”)

Tested on webMethods 8.2.2, Apache 2.2.24 (Unix), PHP 5.3.26