0x7OpenResty系列:Openresty最佳案例| 第5篇:HTTP和C_json模块

举报
云享专家 发表于 2019/09/30 14:18:07 2019/09/30
【摘要】 JSON是一种常见的数据交换格式,常用于HTTP通信协议和其他数据传输领域。在openresty默认内嵌了lua_cjson模块,用来序列化数据。 lua_cjson模块的地址:HTTPS://www.kyne.com.au/~mark/software/lua-cjson-manual.html

HTTP客户端

Openresty没有提供默认的的Http客户端,需要下载第三方的HTTP客户端。

下载LUA-resty-HTTPlualib目录下,使用以下的命令下载:

cd /usr/example/lualib/resty/
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http_headers.lua
 
wget https://raw.githubusercontent.com/pintsized/lua-resty-http/master/lib/resty/http.lua


卢阿 - resty-HTTP模块的地址为https://github.com/pintsized/lua-resty-http

安装成功后,通过require“ resty.http”)日期lua_http模块,它具有以下的api方法:

·      语法:httpc = http.new()创建一个http对象

·      语法:reserr = httpcrequest_uriuriparams)根据参数获取内容,包括:

§  状态码

§  标头响应头

§  身体

vim /usr/example/lua/test_http.lua,写以下代码:

local http = require("resty.http")
 
local httpc = http.new()
 
local resp, err = httpc:request_uri("http://s.taobao.com", {
    method = "GET",
    path = "/search?q=hello",
    headers = {
        ["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36"
    }
})
 
if not resp then
    ngx.say("request error :", err)
    return
end
 
 
ngx.status = resp.status
 
 
for k, v in pairs(resp.headers) do
    if k ~= "Transfer-Encoding" and k ~= "Connection" then
        ngx.header[k] = v
    end
end
 
ngx.say(resp.body)
 
httpc:close()


vim /usr/example/example.conf附加以下的配置:

 location /lua_http {
   default_type 'text/html';
   lua_code_cache on;
   content_by_lua_file /usr/example/lua/test_http.lua;
 }


Nginx的的配置文件nginx.confHTTP部分,加上以下DNS解析:

vim /usr/servers/nginx/conf/nginx.conf

resolver 8.8.8.8;


浏览器访问:HTTP//116.196.177.123/lua_http,浏览器会显示淘宝的搜索页。

lua_cjson模块

JSON是一种常见的数据交换格式,常用于HTTP通信协议和其他数据传输领域。在openresty默认内嵌了lua_cjson模块,用来序列化数据。

lua_cjson模块的地址:HTTPS//www.kyne.com.au/~mark/software/lua-cjson-manual.html

它常用的API如下:

·      当地的cjson =需要“ cjson”获取一个cjson对象

·      本地str = cjson.encodeobjobj转换成字符串

·      本地obj = cjson.decodestr)将字符串转obj

vim /usr/example/lua/test_cjson.lua,添加以下内容:

local cjson = require("cjson")
 
 
local obj = {
    id = 1,
    name = "zhangsan",
    age = nil,
    is_male = false,
    hobby = {"film", "music", "read"}
}
 
local str = cjson.encode(obj)
ngx.say(str, "<br/>")
 
 
str = '{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1,"age":null}'
local obj = cjson.decode(str)
 
ngx.say(obj.age, "<br/>")
ngx.say(obj.age == nil, "<br/>")
ngx.say(obj.age == cjson.null, "<br/>")
ngx.say(obj.hobby[1], "<br/>")


vim /usr/example/example.conf添加以下内容:

 location ~ /lua_cjson {
   default_type 'text/html';
   lua_code_cache on;
   content_by_lua_file /usr/example/lua/test_cjson.lua;
 }


在浏览器***问http://116.196.177.123/lua_cjson,浏览器显示以下内容:

{"hobby":["film","music","read"],"is_male":false,"name":"zhangsan","id":1}
null
false
true
film


 

 

原创作者:方志朋

方志朋简介:SpringCloud中国社区联合创始人,博客访问量突破一千万,爱好开源,热爱分享,活跃于各大社区,保持着非常强的学习驱动力,终身学习践行者,终身学习受益者。目前就职于国内某家知名互联网保险公司,担任DEVOPS工程师,对微服务领域和持续集成领域研究较深,精通微服务框架SpringCloud


【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。