使用nginx capture的注意事项

post

res = ngx.location.capture('/update',{method = ngx.HTTP_POST,body = "name=" .. name .. "&content=" .. content})

解析json

local cjson = require("cjson")
local data = cjson.decode(res.body)

若是返回的不是json格式,则这段lua脚本会报错json

Expected value but found invalid token at character 1
stack traceback:
coroutine 0:
    [C]: in function 'decode'

安全解析json

local cjson = require("cjson.safe")
local data = cjson.decode(res.body)
if data==nil then
 --....
end

doc

相关文章
相关标签/搜索