Welcome to our website.

How to Use lua-resty-http in OpenResty for HTTP POST Requests

When an OpenResty application needs to send HTTP requests from a gateway or Lua layer, lua-resty-http is a practical third-party client library to use. It is built for OpenResty/ngx_lua and supports sending data with POST requests.

Installing lua-resty-http

The library can be installed manually from its project repository:

https://github.com/pintsized/lua-resty-http

After downloading the project, copy these two files from lua-resty-http/lib/resty/:

  • http.lua
  • http_headers.lua

Place them into the following directory:

/usr/local/openresty/lualib/resty

This assumes OpenResty is installed under /usr/local/openresty.

In normal cases, no restart is required after copying the files. In a small number of situations involving OpenResty shared_dict data, a restart may be needed after clearing that data.

Example: Sending a POST Request

The following example shows how to load the library, prepare form data, send a POST request, and read the response:

local function HttpUtil(jwt_data)
    -- 引入第三方库
    local http = require "resty.http"
    local httpc = http.new()

    -- 需要post的参数
    local str = "aaa=1&bbb=2%ccc=3"

    -- 请求地址
    local url = "http://wwww.xxxxxxx.com"

    local res, err = httpc:request_uri(url, {
        method = "POST",
        body = str,
        headers = {
            ["Content-Type"] = "application/x-www-form-urlencoded",
            ["Content-Length"] = #str,
        }
    })

    -- 返回值
    ngx.say(res.body)
    ngx.say(res.status)
    ngx.say(res.headers)

    return res.body,res.status
end

In this snippet:

  • require "resty.http" loads the HTTP client library.
  • http.new() creates a client instance.
  • body = str sends the POST payload.
  • The headers specify form submission with application/x-www-form-urlencoded and include the body length.
  • The response body, status code, and headers are then output through ngx.say.

Request Options

When calling request_uri, several request parameters are available:

<table> <thead> <tr> <th>Parameter</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>version</td> <td>HTTP version number; currently supports 1.0 or 1.1</td> </tr> <tr> <td>method</td> <td>HTTP method</td> </tr> <tr> <td>path</td> <td>Request path</td> </tr> <tr> <td>query</td> <td>Query string, provided either as plain text or as a Lua table</td> </tr> <tr> <td>headers</td> <td>Request header table</td> </tr> <tr> <td>body</td> <td>Request body, either as a string or as an iterator function (see get_client_body_reader)</td> </tr> <tr> <td>ssl_verify</td> <td>Whether to verify that the SSL certificate matches the hostname</td> </tr> </tbody> </table>

Response Fields

If the request succeeds, res contains the following fields:

<table> <thead> <tr> <th>Parameter</th> <th>Description</th> </tr> </thead> <tbody> <tr> <td>status</td> <td>HTTP status code</td> </tr> <tr> <td>reason</td> <td>Short reason phrase for the status</td> </tr> <tr> <td>headers</td> <td>Response headers as a table; headers with the same name are merged into a table</td> </tr> <tr> <td>has_body</td> <td>Boolean value indicating whether a response body is available</td> </tr> <tr> <td>body_reader</td> <td>Iterator function used to read the body as a stream</td> </tr> <tr> <td>read_body</td> <td>Method that reads the entire response body into a string</td> </tr> <tr> <td>read_trailers</td> <td>Method that reads trailers and merges them into the headers after the body is read</td> </tr> </tbody> </table>

For simple requests, accessing res.body and res.status is often enough. For larger responses or streaming scenarios, body_reader and read_body are useful depending on how the data needs to be handled.

Related Posts