0
点赞
收藏
分享

微信扫一扫

openresty nginx转换get为post请求

悬灸人雪洋 2022-03-12 阅读 61

需求

目前后端服务只接收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的方式传递给后端服务器

举报

相关推荐

0 条评论