开发者

POST data in Lua

I'm learning 开发者_如何学编程Lua at the moment. I need to be able to access post and get data. I'm trying to find out how the equivalent of PHP $_POST and $_GET in Lua.


This depends on the web server you are running in, and any intermediary libraries you are using.

In Apache 2.3, using the included mod_lua, it would be

function my_handler(r)
    -- URI params
    local simple, full = r:parseargs()

    -- POST body
    local simple, full = r:parsebody()
end

Where simple is a table of key -> value (what you want most of the time) and full is key -> [value1, value2, ...] for cases of duplicately named params.

Fuller examples are available at http://svn.apache.org/viewvc/httpd/httpd/trunk/modules/lua/test/htdocs/test.lua?revision=728494&view=markup


There are many web-frameworks for Lua, each with its own way of accessing GET and POST.

Probably easiest way to learn Lua for the web-development is to use WSAPI.

To get GET and POST, use wsapi.request in your handler:

require 'wsapi.request'

local handler = function(env)
  local request = wsapi.request.new(env)
  local GET = wsapi.request.GET
  local POST = wsapi.request.POST

  ...
end


There is no equivalent as Lua is not designed as a web scripting language. In what context are you using this (CGI, FCGI, Apache module)? You'll probably need to look into the CGI specification and accessing environment variables and stdin from Lua.


You could always check out Lua4Web https://github.com/schme16/Lua4Web


Reading POST data in traditional html-form or url-encoded format is a mess in Lua. Better you try to use AJAX forms javascript library, so you get your data sent in JSON back to the server where you can easily parse and use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜