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. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.
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.
Learn Lua with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 40
- Lessons
- 159
Frequently Asked Questions
Is the “Handling HTTP Requests in Lua” lesson free?
Yes — any 3 lessons of the Lua Academy learning path, including “Handling HTTP Requests in Lua”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
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