|
| 1 | +Name |
| 2 | +==== |
| 3 | + |
| 4 | +lua-resty-http - Lua HTTP client driver for ngx_lua |
| 5 | + |
| 6 | +Example |
| 7 | +======= |
| 8 | + |
| 9 | + server { |
| 10 | + location /test { |
| 11 | + content_by_lua ' |
| 12 | + local http = require "resty.http" |
| 13 | + local client = http:new() |
| 14 | + client:set_timeout(1000) -- 1 sec |
| 15 | + |
| 16 | + local ok, err = client:connect("checkip.amazonaws.com", 80) |
| 17 | + if not ok then |
| 18 | + ngx.say("failed to connect: ", err) |
| 19 | + return |
| 20 | + end |
| 21 | + |
| 22 | + local res, err = client:request({ path = "/" }) |
| 23 | + if not res then |
| 24 | + ngx.say("failed to retrieve: ", err) |
| 25 | + return |
| 26 | + end |
| 27 | + |
| 28 | + -- close connection, or put it into the connection pool |
| 29 | + if res.headers["connection"] == "close" then |
| 30 | + local ok, err = client:close() |
| 31 | + if not ok then |
| 32 | + ngx.say("failed to close: ", err) |
| 33 | + return |
| 34 | + end |
| 35 | + else |
| 36 | + client:set_keepalive(0, 100) |
| 37 | + end |
| 38 | + |
| 39 | + if res.status >= 200 and res.status < 300 then |
| 40 | + ngx.say("My IP is: " .. res.body) |
| 41 | + else |
| 42 | + ngx.say("Query returned a non-200 response: " .. res.status) |
| 43 | + end |
| 44 | + '; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | +API |
| 49 | +=== |
| 50 | + |
| 51 | +new |
| 52 | +--- |
| 53 | +`syntax: client, err = http:new()` |
| 54 | + |
| 55 | +Creates a HTTP object. |
| 56 | + |
| 57 | +connect |
| 58 | +------- |
| 59 | +`syntax: ok, err = client:connect(host, port?, conf?)` |
| 60 | + |
| 61 | +Attempts to connect to the remote `host` and `port`. Port 80 is assumed, when no port is given. |
| 62 | + |
| 63 | +Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method. |
| 64 | + |
| 65 | +An optional Lua `conf` table can be specified to declare various options: |
| 66 | + |
| 67 | +* `scheme` |
| 68 | +: Specifies a scheme, defaults to "http" or "https", depending on the port. |
| 69 | +* `path` |
| 70 | +: Specifies a default endpoint. |
| 71 | + |
| 72 | +request |
| 73 | +------- |
| 74 | +`syntax: res, err = client:request(opts?)` |
| 75 | + |
| 76 | +Attempts to retrieve data from a remote location. |
| 77 | + |
| 78 | +An optional Lua `opts` table can be specified to declare various options: |
| 79 | + |
| 80 | +* `method` |
| 81 | +: Specifies the request method, defaults to `'get'`. |
| 82 | +* `path` |
| 83 | +: Specifies the path, defaults to `'/'`. |
| 84 | +* `query` |
| 85 | +: Specifies query parameters. Accepts either a string or a Lua table. |
| 86 | +* `headers` |
| 87 | +: Specifies request headers. Accepts a Lua table. |
| 88 | + |
| 89 | +Returns a `res` object containing three attributes: |
| 90 | + |
| 91 | +* `res.status` (number) |
| 92 | +: The resonse status, e.g. 200 |
| 93 | +* `res.headers` (table) |
| 94 | +: A Lua table with response headers |
| 95 | +* `res.body` (string) |
| 96 | +: The plain response body |
| 97 | + |
| 98 | +set_timeout |
| 99 | +---------- |
| 100 | +`syntax: client:set_timeout(time)` |
| 101 | + |
| 102 | +Sets the timeout (in ms) protection for subsequent operations, including the `connect` method. |
| 103 | + |
| 104 | +set_keepalive |
| 105 | +------------ |
| 106 | +`syntax: ok, err = client:set_keepalive(max_idle_timeout, pool_size)` |
| 107 | + |
| 108 | +Puts the current connection immediately into the ngx_lua cosocket connection pool. |
| 109 | + |
| 110 | +You can specify the max idle timeout (in ms) when the connection is in the pool and the maximal size of the pool every nginx worker process. |
| 111 | + |
| 112 | +In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. |
| 113 | + |
| 114 | +Only call this method in the place you would have called the `close` method instead. Calling this method will immediately turn the current connection into the `closed` state. Any subsequent operations other than `connect()` on the current object will return the `closed` error. |
| 115 | + |
| 116 | +get_reused_times |
| 117 | +---------------- |
| 118 | +`syntax: times, err = client:get_reused_times()` |
| 119 | + |
| 120 | +This method returns the (successfully) reused times for the current connection. In case of error, it returns `nil` and a string describing the error. |
| 121 | + |
| 122 | +close |
| 123 | +----- |
| 124 | +`syntax: ok, err = client:close()` |
| 125 | + |
| 126 | +Closes the current connection and returns the status. |
| 127 | + |
| 128 | +In case of success, returns `1`. In case of errors, returns `nil` with a string describing the error. |
| 129 | + |
| 130 | +Licence |
| 131 | +======= |
| 132 | + |
| 133 | +Some code and documentation parts were (shamelessly) taken from [lua-resty-redis](https://github.com/agentzh/lua-resty-redis), MIT, Copyright by Yichun Zhang. |
| 134 | + |
| 135 | +Greatly inspired by [excon](https://github.com/geemus/excon), MIT, Copyright by Wesley Beary. |
| 136 | + |
| 137 | +This code is covered by MIT License. |
| 138 | +(The MIT License) |
| 139 | + |
| 140 | +Copyright (c) 2013 Black Square Media Ltd |
| 141 | + |
| 142 | +Permission is hereby granted, free of charge, to any person obtaining |
| 143 | +a copy of this software and associated documentation files (the |
| 144 | +'Software'), to deal in the Software without restriction, including |
| 145 | +without limitation the rights to use, copy, modify, merge, publish, |
| 146 | +distribute, sublicense, and/or sell copies of the Software, and to |
| 147 | +permit persons to whom the Software is furnished to do so, subject to |
| 148 | +the following conditions: |
| 149 | + |
| 150 | +The above copyright notice and this permission notice shall be |
| 151 | +included in all copies or substantial portions of the Software. |
| 152 | + |
| 153 | +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, |
| 154 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 155 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 156 | +IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 157 | +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 158 | +TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 159 | +SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
0 commit comments