This topic contains 1 reply, has 2 voices, and was last updated by  Eliot Muir 2 years, 5 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:

    dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563

    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:

    --dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563

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

    --dhj--ksJJJJ2323WjYEPJUSTGARBAGE772GD563--

    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 = 'https://friendly.web.server.net/upload'
       local Response, Code = net.http.post{
          url=URL,
          headers=Headers,
          response_headers_format="raw",
          body=Body,
          live=true
       }
       trace(Response)
       if Code == 200 then
          local OutBound = Response
          queue.push(OutBound)
       else 
          error("Our message was rejected")
       end
    

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

You must be logged in to reply to this topic.