【问题标题】:How to create a request object?如何创建请求对象?
【发布时间】:2023-03-28 11:39:01
【问题描述】:

我正在学习使用 lua(wsapi 和 uWSGI)进行 Web 开发。 我正在尝试创建一个请求对象 (https://keplerproject.github.io/wsapi/libraries.html) 以查看它的外观(以及如何使用它)。所以我设置了一个示例代码:

require "wsapi.cgi"

function run(wsapi_env)
  local headers = { ["Content-type"] = "text/html" }
  local function hello_text()
    local result = "<html><body>\n"
    result = result .. "<p> Hello Wsapi !</p>\n"
    result = result .. "<p>PATH_INFO wsapi_env: " .. wsapi_env.PATH_INFO .. "</p>\n"
    -- problematic code
    local req = wsapi.request.new(wsapi_env)
    if req
    then
        --condition to see if req was nill
        result = result .. "<p>PATH INFO_POST : " .. req.POST .. "</p>\n"
        result = result .. "<p>PATH INFO_GET  : " .. req.GET .. "</p>\n"
    else
        result = result .. "<p> No request <\p>\n"
    end
    -- end of the problematic code
    result = result .. "<p><form method=\"post\" action=\"hello.lua\">\n"
    result = result .. "<textarea name=\"message\"> test </textarea>\n"
    result = result .. "</form></p>\n"
    result = result .. "</body></html>\n"
    coroutine.yield(result)
  end
  return 200, headers, coroutine.wrap(hello_text)
end
return run

但是当我使用请求对象时,客户端会收到一个空白页。

如何创建和使用请求对象?

感谢您的回答!

额外问题:如何重定向到另一个页面(到同一域上的静态页面)?

【问题讨论】:

    标签: lua uwsgi wsapi


    【解决方案1】:

    也许这个小代码 sn-p 会有所帮助:

    local ws_request = require "wsapi.request"
    
    function run(wsapi_env)
    local qs = wsapi_env.QUERY_STRING
    print("QUERY_STRING: " .. qs)
    local req = ws_request.new(wsapi_env)
    print(req.params.q)
    end
    

    【讨论】:

      猜你喜欢
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2018-12-25
      • 2017-07-16
      • 2017-03-08
      • 1970-01-01
      相关资源
      最近更新 更多