This topic contains 1 reply, has 2 voices, and was last updated by  Eliot Muir 8 years, 3 months ago.

Using Iguana to post multipart form data

  • This week one of our customers asked for help using Iguana to post X12 messages to a web service that expects a POST of multi-part form data. (This is the same thing that happens with a web form in a browser, when you type values in some fields, and alongside those fields you also upload one or more files using a “browse” button.)

    The process is similar to sending multipart email messages, but we thought it might be helpful to put up a separate note about it.

    Boundary strings are the trickiest part

    As the name suggests, a multi-part HTTP POST request is made up of many parts. In between those parts are boundary strings. These are just chunks of garbage text, chosen so there’s no danger of mixing them up with the real data of your message. Here’s a sample boundary string:


    In a multipart POST request, you declare the boundary string at the top, and each time you use it, you add hyphens to indicate whether it’s a divider (meaning it will be followed by another part), or the last one (meaning the end of the POST).

    If it’s a middle, it gets two hyphens at the start:


    If it’s an end, it gets two hyphens at the start PLUS two at the end:


    With that in mind, here’s a snippet of Translator code that shows a very simple way to build up and send a multipart POST. (In this example, Data is the X12 message itself.)

       local Headers = {
          ['Content-Type']='multipart/form-data; boundary=dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563'
       local Body =   '--dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563' .. '\r\n'
       Body = Body .. 'Content-Disposition: form-data; name="FirstField"' .. '\r\n\r\n'
       Body = Body .. 'hello all you friends' .. '\r\n'
       Body = Body .. '--dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563' .. '\r\n'
       Body = Body .. 'Content-Disposition: form-data; name="SecondField"' .. '\r\n\r\n'
       Body = Body .. 'hello again you friends' .. '\r\n'
       Body = Body .. '--dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563' .. '\r\n'
       Body = Body .. 'Content-Disposition: form-data; name="file"; filename="X12Message.txt"' .. '\r\n'
       Body = Body .. 'Content-Type: text/plain' .. '\r\n\r\n'
       Body = Body .. Data .. '\r\n'
       Body = Body .. '--dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563--'
       Headers['Content-Length'] = string.len(Body)
       local URL = ''
       local Response, Code ={
       if Code == 200 then
          local OutBound = Response
          error("Our message was rejected")

    Cool! I know of few instances of people doing this very interface.

You must be logged in to reply to this topic.