#lovr #c #extension #plugin #library #http #client #http-client #gamedev
|
1 hónapja | |
---|---|---|
CMakeLists.txt | 1 hónapja | |
LICENSE | 1 éve | |
README.md | 1 éve | |
http.c | 5 hónapja |
A Lua library for performing HTTP(S) requests. It's basically a teeny tiny version of lua-https included by default in LÖVR. Although lovr's in the name, the library is self-contained and it should work in any Lua program.
http = require 'http'
status, data = http.request('https://zombo.com')
print('welcome')
print(status)
print(data)
The module has one function:
status, data, headers = http.request(url, [options])
url
is the URL to request. If it doesn't have a protocol, then http://
will be added.
options
is optional, and is used for advanced request settings.
options.method
is the HTTP method to use, also called the verb. GET
is used by default if
there's no data in the request, otherwise it defauls to POST
. It will be converted to all-caps.
options.data
is the data to send to the server, also called the body. It can be a few different
types:
data
is nil, no request body will be sent (and method
will default to GET
).data
is a string, the string will be used directly as the request body.data
is a table, then pairs in the table will be URL encoded and concatenated together to
form an application/x-www-form-urlencoded
body. For example, if data is { n = 10, k = 'v!' }
,
then the request body will be something like k=v%21&n=10
. Keys can appear in any order. Table
pairs will only be used if the key is a string and the value is a string or number.data
is a lightuserdata, the data pointed to by the lightuserdata will be used as the
request body. Additionally, the datasize
option should be an integer indicating how big the
request body is, in bytes.When options.data
is set, the Content-Type
request header will default to
application/x-www-urlencoded
unless it's set to something else.
options.headers
is a table of request headers to send to the server. Pairs in the table will only
be used if the key is a string and the value is a string or number.
If an error occurs, the function returns nil, errormessage
.
Otherwise, 3 values are returned:
status
is an integer with the HTTP status code (200 is OK, 404 is Not Found, etc.).data
is a string with the data sent by the server (HTML, JSON, binary, etc.).headers
is a table of response headers.multipart/form-data
request bodies are not supported.Authorization
request header instead.The build system is CMake. The CMake script doesn't have any logic to link against Lua yet, so it
will only build properly in LÖVR's plugins
folder, where it will automatically use LÖVR's copy of
Lua.
lovr-http
uses system-provided HTTP libraries:
libcurl4
package, but most systems have it).The system's certificates are used for HTTPS.
MIT, see the LICENSE file for details.