获取nginx lua插件中的json数据
以下是一个使用ngx_lua模块的示例代码,用于获取nginx lua插件中http接口返回的json数据中的username值:
local http = require "resty.http" local cjson = require "cjson.safe" local httpc = http.new() local res, err = httpc:request_uri("<http://example.com/api>", { method = "GET", headers = { ["Content-Type"] = "application/json", } }) if not res then ngx.log(ngx.ERR, "failed to request: ", err) ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end if res.status ~= ngx.HTTP_OK then ngx.log(ngx.ERR, "request failed with status ", res.status) ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR) end local data = cjson.decode(res.body) local username = data.username ngx.say(username)
注意:这个示例代码假设http接口返回的json数据中确实包含了一个名为username的字段,如果不是这个字段,请将代码中的username改为实际的字段名称。
以下是一个使用ngx_lua模块的示例代码,用于获取nginx lua插件中http接口返回的json数据中的username值: local http = require "resty.http" local cjson = require "cjson.safe" local httpc = http.new() local res, err = httpc:request_uri("下一篇:
单链表的反转(C语言实现)