使用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

相關文章
相關標籤/搜索