0Pricing
Lua Academy · Lesson

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)
end

All lessons in this course

  1. OpenResty Architecture
  2. Handling HTTP Requests in Lua
  3. Cosockets and Non-Blocking TCP
  4. Shared Memory Dictionaries
← Back to Lua Academy