lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 24 October 2011 12:09, Pierre Chapuis <catwell@archlinux.us> wrote:
> On 24.10.2011 17:33, Peng Zhicheng wrote:
>
>>> I'm trying to send a web form to a server with Lua Socket but I did
>>> not find any example. I found only your post in Lua list. Did you
>>> resolve your problem? Can you send me an example of how I can send a
>>> HTTP POST with parameters (variables)?

Using LuaSocket's simple interface:

   http.request(url, form_data)

The form data must be urlencoded key=value pairs. We have a function
you can use to do this at
http://hg.prosody.im/trunk/file/0ed617f58404/net/http.lua#l31
(formencode depends on _formencodepart).

  local form_data = formencode({ field1 = value1, field2 = value2 })

The standards say you /should/ return the fields in the order they
were in in the original form. If you want to preserve order you can
use this syntax:

  local form_data = formencode({ { "field1", "value1" }, { "field2",
"value2" } })

>    local rq_resp = {}
>    local b,c,h = http.request {
>      method = "POST",
>      url = my_url,
>      headers = {},
>      source = ltn12.source.string(rq_body),
>      sink = ltn12.sink.table(rq_resp),
>    }
>

This is LuaSocket's "generic" interface, which also works. When you're
using this to POST, you must also set headers = { ["Content-Type"] =
"application/x-www-form-urlencoded" }.

Regards,
Matthew