Handling HTTP Requests in Lua
Access request headers, body, args, and craft responses with ngx API.
Handling HTTP Requests in Lua is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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)
endReading Query Parameters
ngx.req.get_uri_args() returns a table of URL query parameters.
local args = ngx.req.get_uri_args()
local page = tonumber(args.page) or 1
local size = tonumber(args.size) or 20
ngx.say("Page: " .. page .. ", Size: " .. size)Reading Request Body
Read the request body after explicitly calling ngx.req.read_body().
ngx.req.read_body()
local body = ngx.req.get_body_data()
if not body then
ngx.status = 400
ngx.say("Empty body")
return
endParsing JSON Body
Use a JSON library (e.g., cjson) to parse the request body.
local cjson = require("cjson")
ngx.req.read_body()
local body = ngx.req.get_body_data()
local ok, data = pcall(cjson.decode, body)
if not ok then
ngx.status = 400
ngx.say("Invalid JSON")
return ngx.exit(400)
endSetting Response Headers
Set response headers via ngx.header before calling ngx.say or ngx.print.
ngx.header["Content-Type"] = "application/json"
ngx.header["X-Request-ID"] = ngx.var.request_idWriting the Response
ngx.say(s) writes a string and a newline. ngx.print(s) writes without a newline. ngx.exit(code) terminates the request with the given HTTP status.
ngx.status
Set the HTTP status code via ngx.status = 404 before writing the response body.
ngx.status = 404
ngx.header["Content-Type"] = "application/json"
ngx.say(cjson.encode({error = "Not Found"}))Internal Redirects
ngx.exec("/other/path") performs an internal redirect to another Nginx location block without an extra HTTP round-trip.
Subrequests
ngx.location.capture("/api") makes a subrequest to another location and returns a table with status, headers, and body.
local res = ngx.location.capture("/internal/auth")
if res.status ~= 200 then
ngx.exit(403)
endRequest Context Sharing
ngx.ctx is a per-request table for sharing data between different phase handlers (access → content → log).
HTTP Handling Question
Which function reads URL query parameters in OpenResty?
Recap: Handling HTTP Requests
Use ngx.req functions to read method, headers, query args, and body. Set ngx.header and ngx.status before writing. Use ngx.say/ngx.print for output. Share per-request data via ngx.ctx.
Frequently asked questions
Is the “Handling HTTP Requests in Lua” lesson free?
Yes — the full text of “Handling HTTP Requests in Lua” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Handling HTTP Requests in Lua”?
Access request headers, body, args, and craft responses with ngx API. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Lua Academy?
No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Handling HTTP Requests in Lua” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Lua Academy lesson?
Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- OpenResty Architecture
- Handling HTTP Requests in Lua
- Cosockets and Non-Blocking TCP
- Shared Memory Dictionaries