title: execute http post with Lua
body: <p>I have written a Python script which POSTs data to an apache webserver and the data arrives nicely in $_POST and $_FILES aray. Now I want to implement this same thing in Lua but I can't get it going yet.</p>
  
  <p>My code in Python looks something like this:</p>
  
  <pre><code>    try:
          wakeup()
          socket.setdefaulttimeout(TIMEOUT)
          opener = urllib2.build_opener(MultipartPostHandler.MultipartPostHandler)
          host = HOST
          func = "post_img"
          url = "http://{0}{1}?f={2}&amp;nodemac={3}&amp;time={4}".format(host, URI, func, nodemac, timestamp)
          if os.path.isfile(filename):
              data = {"data":open(filename,"rb")}
              print "POST time "+str(time.time())
              response = opener.open(url, data, timeout=TIMEOUT)
              retval = response.read()
              if "SUCCESS" in retval:
                  return 0
              else:
                  print "RETVAL: "+retval
                  return 99
      except Exception as e:
          print "EXCEPTION time "+str(time.time())+" - "+str(e)
          return 99
  </code></pre>
  
  <p>The Lua code I have come up with thus far:</p>
  
  <pre><code>#! /usr/bin/lua
  
  http = require("socket.http")
  ltn12 = require("ltn12")
  
  http.request{
      url = "localhost/test.php?test=SEMIOS",
      method = "POST",
      headers = {
          ["Content-Type"] =  "multipart/form-data; boundary=127.0.1.1.1000.17560.1375897994.242.1",
          ["Content-Length"] = 7333
      },
      source = ltn12.source.file(io.open("test.gif")),
      sink = ltn12.sink.table(response_body)
  }
  print(response_body[1]) --response to request
  </code></pre>
  
  <p>but this code keeps getting me this on execution:</p>
  
  <pre><code>$ ./post.lua
  /usr/bin/lua: ./post.lua:17: attempt to index global 'response_body' (a nil value)
  stack traceback:
          ./post.lua:17: in main chunk
          [C]: ?
  reg@DesktopOffice:~$ 
  </code></pre>