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">

</form>

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):

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

%variableWithTheXMLString% 

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

%variableWithTheUserID%

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

%variableWithThePassword%

----=Part--123456789--

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

  1. Hi Niklas,

    Could you please let me know how this is achieved in detail with the body content.

    I am also trying to do the similar post by seeing 500 : Internal Server Error

    Thanks,
    KK

    • I was never successful with the built-in pub.mime:createMimeData,pub.mime:addBodyPart and pub.mime:getEnvelopeStream services. The solution here is using a manual created MIME message instead.

  2. If you have access to PSUtilities package (a free no-support package from wM), there is an example of how to create MIME data. PSUtilities.email:smtp

  3. Hi Niklas,

    How did you created MIME message manually? Please suggest the steps

    • Hi! The MIME message is just a formated string. The format is like my example above (“My MIME document”). After this I just set the headers, like above example and send the formated string via the built in webmethods http service. If all is formated correctly the form will accept it as a MIME document. Is there anything specific you are having problem with?

      • Hi could you more detail about how you created mime data? if my understading is correct
        you put the “mime doc” in string then map to pub.client.http – data – string ?

        • Yes, the ‘mime doc’ is just a string, here with newlines to make it easier to read, but the data type is a normal webMethods string. The same as you would use if you wanted to map or save your name. The contents of the ‘mime doc’ are all normal ASCII characters. No magic at all 😉

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" line="" escaped="" cssfile="">

This site uses Akismet to reduce spam. Learn how your comment data is processed.