Handling HTTP Requests in Lua
Access request headers, body, args, and craft responses with ngx API.
Reading Request Method and URI
Access the HTTP method and URI from the ngx namespace.
-- content_by_lua_block
local method = ngx.req.get_method()
local uri = ngx.var.uri
ngx.say("Method: " .. method .. ", URI: " .. uri)Reading Request Headers
ngx.req.get_headers() returns a table of request headers.
local headers = ngx.req.get_headers()
local auth = headers["Authorization"]
if not auth then
ngx.status = 401
ngx.say("Unauthorized")
ngx.exit(ngx.HTTP_UNAUTHORIZED)
endAll lessons in this course
- OpenResty Architecture
- Handling HTTP Requests in Lua
- Cosockets and Non-Blocking TCP
- Shared Memory Dictionaries