Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use http and C_json modules in Openresty

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use the http and C_json modules in Openresty. It is very detailed and has a certain reference value. Friends who are interested must finish it!

Http client

Openresty does not provide a default Http client, so you need to download a third-party http client.

Download lua-resty-http to the lualib directory and download it using the following command:

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

The address of the lua-resty-http module is https://github.com/pintsized/lua-resty-http

After successful installation, the lua_http module is introduced through require ("resty.http"), which has the following api methods:

Syntax: httpc = http.new () to create a http object

Syntax: res, err = httpc:request_uri (uri, params) gets the content based on the parameters, including:

Status status code

Headers response header

Body response body

Vim / usr/example/lua/test_http.lua, write the following code:

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 plus the following configuration:

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

In the http section of Nginx's configuration file nginx.conf, add the following dns resolution:

Vim / usr/servers/nginx/conf/nginx.conf

Resolver 8.8.8.8

Browser access: the http://116.196.177.123/lua_http, browser will display Taobao's search page.

Lua_cjson module

Json is a common data exchange format, which is often used in http communication protocol and other data transmission fields. The lua_cjson module is embedded in openresty by default to serialize data.

Address of lua_cjson module: https://www.kyne.com.au/~mark/software/lua-cjson-manual.html

The commonly used API is as follows:

Local cjson = require "cjson" to get a cjson object

Local str = cjson.encode (obj) obj converted to string

Local obj = cjson.decode (str) convert string to obj

Vim / usr/example/lua/test_cjson.lua, add the following:

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, ") str ='{" hobby ": [" film "," music "," read "]," is_male ": false," name ":" zhangsan " "id": 1, "age": null} 'local obj = cjson.decode (str) ngx.say (obj.age, ") ngx.say (obj.age = = nil,") ngx.say (obj.age = = cjson.null, ") ngx.say (obj.hobby [1],")

Vim / usr/example/example.conf adds the following:

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

Access http://116.196.177.123/lua_cjson on the browser, and the browser displays the following:

{"hobby": ["film", "music", "read"], "is_male": false, "name": "zhangsan", "id": 1} nullfalsetruefilm is all the content of this article "how to use http and C_json modules in Openresty". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report