Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adamharrison/lite-xl-www
Repository containing a library that can be used with lite-xl to send HTTP requests.
https://github.com/adamharrison/lite-xl-www
Last synced: 1 day ago
JSON representation
Repository containing a library that can be used with lite-xl to send HTTP requests.
- Host: GitHub
- URL: https://github.com/adamharrison/lite-xl-www
- Owner: adamharrison
- Created: 2023-04-30T04:06:40.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-10-16T21:34:04.000Z (about 1 month ago)
- Last Synced: 2024-10-18T21:25:50.114Z (about 1 month ago)
- Language: C
- Size: 2.13 MB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lite-xl-www
A simple library that provides a web client to fetch, and submit data via HTTP(S)
requests.Functions via lite-xl's coroutine mechanism. Normally, when a request is
submitted, it will be transferred in the background, and until it is ready,
will continually yield within the coroutine. If you submit a request outside
a coroutine, the request will block the editor, but is still feasible to submit
in cases where that is not an issue.## Building
To build the library, from scratch, do:
```sh
git clone --depth=1 https://github.com/adamharrison/lite-xl-www.git && \
cd lite-xl-www && git submodule update --init && build.sh
```This should spit out a shared library on linux, windows (msys2), macos and android.
## Sample Basic Usage
See below an example:
```lua
local www = require "libraries.www"core.add_thread(function()
local agent = www.new()
print(agent:get("https://google.com"))
print(agent:post("https://google.com", "q=test"))
end)
```## Sample Advanced Usage
For a more nitty-gritty experience, you can use the core method `request`, which
uses a stateless interface, and will return you the raw request of what you
request, no redirection, decoding, or anything else.The only shared state present is the underlying queue that handles making
the requests, and the SSL configuration. Everything else is stateless.```lua
local www = require "libraries.www"core.add_thread(function()
local res = www.request({
url = "https://google.com",
headers = { connection = "close" }
})
print(res.code)
print(res.status)
for k,v in pairs(res.headers) do
print(k, v)
end
print(res.body)
end)
```In the case where you want to retrieve, or submit a large document, you can
do things via chunks.```lua
local www = require "libraries.www"core.add_thread(function()
local f = io.open("mylargefile")
local res = www.request({
url = "https://google.com",
method = "POST",
body = function()
return f:read(4096)
end
})
print(res.status)
end)
```And, the inverse, for the download:
```lua
local www = require "libraries.www"core.add_thread(function()
local f = io.open("mylargefile", "wb")
www.request({
url = "https://google.com",
response = function(response, chunk)
print(response.status)
f:write(chunk)
end
})
end)
```