美文网首页
2018年9月15——用postman发送post请求方式带js

2018年9月15——用postman发送post请求方式带js

作者: 兔子Tony的主人 | 来源:发表于2018-09-15 20:13 被阅读0次

注意:前提是安装openresty,如果是其他的nginx+lua的安装方式,不保证没有坑哦。

场景

用户登录点击登录后,将用户名密码发送给nginx+lua并进行处理

问题

怎么用postman发送出post请求方式,带json,让后台能顺利接收
nginx+lua接收到后,怎么获取postman发送出来的数据

实现

  • 首先怎么用postman发送

    第一步,主要是在Headers中添加Content-type为application/json snipaste20180915_195345.png
    第二步,在Body中添加数据 snipaste20180915_195707.png

    做完这些工作,就可以点击send进行发送了。

  • nginx + lua接收
    nginx的部分代码如下,
location ^~ /api/getuserinfo {
    content_by_lua_file "/usr/local/openresty/nginx/conf/getuserinfo";
}

lua文件getuserinfo的代码如下

--引入openresty自带的json处理对象
local cjson = require("cjson")
--还不知道为什么要有这句,但是没有,下边拿不到json的哦
ngx.req.read_body()
--获取json
local args = ngx.req.get_body_data()
--打印下请求方式,的确是post
ngx.say(ngx.var.request_method)
--打印下postman发送过来的json全貌,就不截图了,和postman窗口自己创建的json一样
ngx.say(ngx.req.get_body_data())
--用cjson的decode方法处理json,使我们可以获取到具体的json中包含的值
local res_tab = cjson.decode(args)
--打印其中的userName,得到值123
ngx.say(res_tab["userName"])
--这是一个错误的写法,最终打印nil
ngx.say(ngx.req.get_body_data().userName)

结果上图


snipaste20180915_201155.png

相关文章

网友评论

      本文标题:2018年9月15——用postman发送post请求方式带js

      本文链接:https://www.haomeiwen.com/subject/zoutnftx.html