在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):tokers/lua-resty-requests开源软件地址(OpenSource Url):https://github.com/tokers/lua-resty-requests开源编程语言(OpenSource Language):Lua 90.5%开源软件介绍(OpenSource Introduction):Namelua-resty-requests - Yet Another HTTP Library for OpenResty. resty -e 'print(require "resty.requests".get{ url = "https://github.com", stream = false }.content)' Table of Contents
StatusThis Lua module now can be considered as production ready. Note since the Features
Synopsislocal requests = require "resty.requests"
-- example url
local url = "http://example.com/index.html"
local r, err = requests.get(url)
if not r then
ngx.log(ngx.ERR, err)
return
end
-- read all body
local body = r:body()
ngx.print(body)
-- or you can iterate the response body
-- while true do
-- local chunk, err = r:iter_content(4096)
-- if not chunk then
-- ngx.log(ngx.ERR, err)
-- return
-- end
--
-- if chunk == "" then
-- break
-- end
--
-- ngx.print(chunk)
-- end
-- you can also use the non-stream mode
-- local opts = {
-- stream = false
-- }
--
-- local r, err = requests.get(url, opts)
-- if not r then
-- ngx.log(ngx.ERR, err)
-- end
--
-- ngx.print(r.content)
-- or you can use the shortcut way to make the code cleaner.
local r, err = requests.get { url = url, stream = false } Installation$ luarocks install lua-resty-requests
$ opm get tokers/lua-resty-requests
Just tweeks the
Methodsrequestsyntax: local r, err = requests.request(method, url, opts?) This is the pivotal method in The first parameter
The second parameter The third param, an optional Lua table, which contains a number of options:
You can use the method requests.state to get the textual meaning of these values.
Note this is still unstable, caution should be exercised. Also, there are some limitations, see lua-resty-http2 for the details.
{
http = { host = "127.0.0.1", port = 80 },
https = { host = "192.168.1.3", port = 443 },
} When using HTTPS proxy, a preceding CONNECT request will be sent to proxy server.
you can assign Lua functions to hooks, these functions accept the response object as the unique param. local hooks = {
response = function(r)
ngx.log(ngx.WARN, "during requests process")
end
} Considering the convenience, there are also some "short path" options:
{
user = "alex",
pass = "123456"
} Request header
{
["PHPSESSID"] = "298zf09hf012fh2",
["csrftoken"] = "u32t4o3tb3gg43"
} The
statesyntax: local state_name = requests.state(state) The method is used for getting the textual meaning of these values:
a Lua string getsyntax: local r, err = requests.get(url, opts?) Sends a HTTP GET request. This is identical with requests.request("GET", url, opts) headsyntax: local r, err = requests.head(url, opts?) Sends a HTTP HEAD request. This is identical with requests.request("HEAD", url, opts) postsyntax: local r, err = requests.post(url, opts?) Sends a HTTP POST request. This is identical with requests.request("POST", url, opts) putsyntax: local r, err = requests.put(url, opts?) Sends a HTTP PUT request. This is identical with requests.request("PUT", url, opts) deletesyntax: local r, err = requests.delete(url, opts?) Sends a HTTP DELETE request. This is identical with requests.request("DELETE", url, opts) optionssyntax: local r, err = requests.options(url, opts?) Sends a HTTP OPTIONS request. This is identical with requests.request("OPTIONS", url, opts) patchsyntax: local r, err = requests.patch(url, opts?) Sends a HTTP PATCH request. This is identical with requests.request("PATCH", url, opts) Response ObjectMethods like
This function accepts an optional param In case of failure,
In case of failure,
Note When HTTP/2 protocol is applied, the SessionA session persists some data across multiple requests, like cookies data, authorization data and etc. This mechanism now is still experimental. A simple example: s = requests.session()
local r, err = s:get("https://www.example.com")
ngx.say(r:body()) A session object has same interfaces with TODO
AuthorAlex Zhang (张超) [email protected], UPYUN Inc. Copyright and LicenseThe bundle itself is licensed under the 2-clause BSD license. Copyright (c) 2017-2019, Alex Zhang. This module is licensed under the terms of the BSD license. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. See Also
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论