需求
目前后端服务只接收post的json格式,但是前端为get请求,因此需要对数据进行转换
配置
server {
listen 20080;
rewrite_by_lua_block {
local cjson = require "cjson"
ngx.req.read_body()
local getargs = ngx.req.get_uri_args()
ngx.req.set_body_data(cjson.encode(getargs))
}
proxy_set_header 'Content-Type' 'application/json';
proxy_method POST;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
效果
前端通过http请求后,lua会把get请求的参数转换为json格式通过post的方式传递给后端服务器