init.lua 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. return {
  2. tag = 'libraries',
  3. summary = 'HTTP(S) requests.',
  4. description = [[
  5. The [lovr-http](https://github.com/bjornbytes/lovr-http) plugin performs HTTP requests.
  6. First, `require` the plugin and save it into a variable: `local http = require 'http'`.
  7. The module has one function:
  8. status, data, headers = http.request(url, [options])
  9. This will perform an HTTP request and block until the request is complete.
  10. ### Arguments
  11. `url` is the URL to request. If it doesn't have a protocol, then `http://` will be added.
  12. `options` is optional, and is used for advanced request settings.
  13. `options.method` is the HTTP method to use, also called the verb. `GET` is used by default if
  14. there's no data in the request, otherwise it defauls to `POST`. It will be converted to
  15. all-caps.
  16. `options.data` is the data to send to the server, also called the body. It can be a few
  17. different types:
  18. - When `data` is nil, no request body will be sent (and `method` will default to `GET`).
  19. - When `data` is a string, the string will be used directly as the request body.
  20. - When `data` is a table, then pairs in the table will be URL encoded and concatenated together
  21. to form an `application/x-www-form-urlencoded` body. For example, if data is
  22. `{ n = 10, k = 'v!' }`, then the request body will be something like `k=v%21&n=10`. Table
  23. pairs will only be used if the key is a string and the value is a string or number.
  24. - When `data` is a lightuserdata, the data pointed to by the lightuserdata will be used as the
  25. request body. Additionally, the `datasize` option should be an integer indicating how big the
  26. request body is, in bytes.
  27. When `options.data` is set, the `Content-Type` request header will default to
  28. `application/x-www-urlencoded` unless it's set to something else.
  29. `options.headers` is a table of request headers to send to the server. Pairs in the table will
  30. only be used if the key is a string and the value is a string or number.
  31. ### Returns
  32. If an error occurs, the function returns `nil, errormessage`.
  33. Otherwise, 3 values are returned:
  34. - `status` is an integer with the HTTP status code (200 is OK, 404 is Not Found, etc.).
  35. - `data` is a string with the data sent by the server (HTML, JSON, binary, etc.).
  36. - `headers` is a table of response headers.
  37. ]],
  38. external = true,
  39. notes = [[
  40. On Linux, this module requires the `curl` library to be installed. Example on Debian-based
  41. distributions:
  42. sudo apt install libcurl4
  43. ]],
  44. example = [[
  45. local http = require 'http'
  46. local status, data, headers = http.request('https://zombo.com')
  47. print('welcome')
  48. print(status)
  49. print(data)
  50. print('headers:')
  51. for k, v in pairs(headers) do
  52. print('\t' .. k, v)
  53. end
  54. ]]
  55. }