This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
api:internet [2019/06/15 09:24] payonel |
api:internet [2019/11/29 23:06] jab [Internet API] |
||
---|---|---|---|
Line 13: | Line 13: | ||
| | ||
Example specifying PUT: | Example specifying PUT: | ||
- | `internet.request("https://example.com", "put data", {}, "PUT")` to send a request through HTTPS. | + | `internet.request("https://example.com", "put data", {}, "PUT")`. |
- `internet.socket(address:string[, port:number]):table` | - `internet.socket(address:string[, port:number]):table` | ||
Line 30: | Line 30: | ||
handle:close() | handle:close() | ||
``` | ``` | ||
+ | If you need the HTTP response code, message, and headers, they are retrieved from the internal object, which is stored in the metatable of the returned object. | ||
+ | ```lua | ||
+ | -- https://github.com/kikito/inspect.lua/blob/master/inspect.lua | ||
+ | local inspect = require("inspect") | ||
+ | local internet = require("internet") | ||
+ | |||
+ | local handle = internet.request("https://www.google.com") | ||
+ | local result = "" | ||
+ | for chunk in handle do result = result..chunk end | ||
+ | -- Print the body of the HTTP response | ||
+ | -- print(result) | ||
+ | |||
+ | -- Grab the metatable for the handle. This contains the | ||
+ | -- internal HTTPRequest object. | ||
+ | local mt = getmetatable(handle) | ||
+ | |||
+ | -- The response method grabs the information for | ||
+ | -- the HTTP response code, the response message, and the | ||
+ | -- response headers. | ||
+ | local code, message, headers = mt.__index.response() | ||
+ | print("code = "..tostring(code)) | ||
+ | print("message = "..tostring(message)) | ||
+ | print(inspect(headers)) | ||
+ | ``` | ||
This is an example of a basic IRC bot that echos back what you say to it, using the sockets in the internet api. | This is an example of a basic IRC bot that echos back what you say to it, using the sockets in the internet api. |