#### This chapter mainly introduces how to obtain request parameters and return data after processing.
We know that http requests are usually divided into two types, namely GET and POST. In the http protocol, GET parameters are usually followed by uri. , and the POST request parameters are included in the request body. By default, nginx will not read the POST request parameters. It is best not to try to change this behavior, because in most cases, the POST request is sent after the To process it, nginx only needs to read the request uri part and the request header.
Because of this design, there are two ways to get request parameters:
GET
```
local args = ngx.req.get_uri_args() -- here is a table , including all get request parameters
local id = ngx.var.arg_id -- here gets a single request parameter, but if this parameter is not passed, an error will be reported, the above method is recommended
```
POST
```
ngx.req. read_body() -- first read the request body
local args = ngx.req.get_post_args() -- here is also a table that contains all post request parameters
```
You can get the http request method through the following method
```
local request_method = ngx.var.request_method -- GET or POST
```
In order to unify the way to obtain request parameters, hide specific details, and provide a more friendly api interface, we can simply encapsulate it
lua/req.lua
```
local _M = {}
-- get http get/post request parameters
function _M.getArgs()
local request_method = ngx.var.request_method
local args = ngx.req.get_uri_args()
-- get parameters
if "POST" == request_method then
ngx.req.read_body()
local postArgs = ngx.req.get_post_args()
if postArgs then
for k, v in pairs(postArgs) do
args[k] = v
end
end
end
return args
end
return _M
````
This module realizes the acquisition of parameters, and supports GET, POST two parameter transmission methods, and post requests with parameters placed in uri and body, which will combine the parameters submitted by the two methods
Next we can write a simple lua to introduce this module, and then test the effect
conf/nginx.conf
```
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
lua_package_path /Users/Lin /opensource/openresty-web-dev/demo2/lua/?.lua; # Be sure to specify package_path here, otherwise the imported module will not be found, then 500
server {
listen 80;
server_name localhost;
lua_code_cache off;
location ~ / lua/(.+) {
default_type text/html;
content_by_lua_file lua/$1.lua;
}
}
}
```
lua/hello.lua
```
local req = require "req"
local args = req.getArgs()
local name = args['name']
if name == nil or name == "" then
name = "guest"
end
ngx.say("<p>hello " .. name .. "!</p>")
```
Test
http://localhost/lua/hello?name=Lin
output hello Lin!
http://localhost/lua/hello
output hello guest!
ok Here, we have been able to The parameters of the request, and after doing some processing, the data is returned
[sample code](https://github.com/362228416/openresty-web-dev) See demo2 section
Openresty front-end development introduction 2
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=326383023&siteId=291194637
Ranking