Skip to content

Commit 62059dd

Browse files
att
1 parent feb4c4f commit 62059dd

1 file changed

Lines changed: 134 additions & 130 deletions

File tree

README.md

Lines changed: 134 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1,178 @@
1-
# LuaBear 🐻
1+
# LuaBear: HTTP Client Library for Lua
22

3-
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
4-
[![Lua Version](https://img.shields.io/badge/Lua-5.1%2B-blue.svg)](https://www.lua.org/)
5-
[![Release](https://img.shields.io/github/v/release/OUIsolutions/Lua-Bear)](https://github.com/OUIsolutions/Lua-Bear/releases)
6-
[![Downloads](https://img.shields.io/github/downloads/OUIsolutions/Lua-Bear/total)](https://github.com/OUIsolutions/Lua-Bear/releases)
3+
LuaBear is a powerful and easy-to-use HTTP client library for Lua, designed to make HTTP requests and handle responses with a simple and intuitive API. It supports sending and receiving data in various formats, including JSON, binary, and file streams, and allows for custom headers and flexible request options.
74

8-
> A simple, powerful HTTP client library for Lua
5+
## Features
6+
- Simple HTTP(S) requests (GET, POST, etc.)
7+
- Read response as string, chunked, or JSON
8+
- Send data as string, JSON, binary, or file stream
9+
- Custom request headers
10+
- Easy integration with Lua projects
911

10-
## 📋 Overview
12+
## Installation
13+
To install the lib, just download the lib [Lib Download](https://github.com/OUIsolutions/Lua-Bear/releases/download/0.1.0/luaBear.zip)
14+
in the root of your project, and run with:
1115

12-
LuaBear is a user-friendly HTTP client library that makes sending web requests in Lua simple and straightforward. Whether you're a beginner or experienced developer, LuaBear provides an easy way to interact with web services and APIs.
16+
```lua
17+
local luabear = require("luaBear.luaBear")
1318

14-
## ✨ Features
19+
```
1520

16-
-**Simple HTTP/HTTPS requests** (GET, POST, PUT, DELETE, etc.)
17-
-**Flexible response handling** - read as string, chunks, or JSON
18-
-**Multiple data formats** - send text, JSON, binary data, or files
19-
-**Custom headers** - set any HTTP headers you need
20-
-**Beginner-friendly API** - intuitive interface for new users
2121

22-
## 🚀 Installation
2322

24-
### Option 1: Direct Download (Recommended for Beginners)
23+
#### Installation from Command
24+
if you prefer to install with a command, just type:
25+
```shell
26+
curl -L -o Lua-Bear.zip https://github.com/OUIsolutions/Lua-Bear/releases/download/0.1.0/luaBear.zip && unzip Lua-Bear.zip && rm Lua-Bear.zip
27+
```
2528

26-
1. Download the library package:
27-
```
28-
mkdir -p luaBear
29-
curl -L -o luaBear/luaBear.lua https://github.com/OUIsolutions/Lua-Bear/releases/download/0.1.2/luaBear.lua
30-
curl -L -o luaBear/luaBear.so https://github.com/OUIsolutions/Lua-Bear/releases/download/0.1.2/luaBear.so
31-
```
3229

33-
4. Use in your Lua project:
34-
```lua
35-
local luabear = require("luaBear.luaBear")
36-
```
30+
#### Build from scratch
3731

38-
### Option 2: Manual Download
32+
For building from scratch you need to have Darwin installed on your machine. To install Darwin:
33+
```bash
34+
curl -L https://github.com/OUIsolutions/Darwin/releases/download/0.020/darwin.out -o darwin.out && chmod +x darwin.out && sudo mv darwin.out /usr/bin/darwin
35+
```
3936

40-
1. Download the library from [GitHub Releases](https://github.com/OUIsolutions/Lua-Bear/releases/download/0.1.0/luaBear.zip)
41-
2. Extract the ZIP file to your project directory
42-
3. Import in your code:
43-
```lua
44-
local luabear = require("luaBear.luaBear")
45-
```
37+
You'll also need Docker (default) or Podman installed on your system.
4638

47-
### Option 3: Build from Source
39+
Then you can build the project in the root directory of the project:
4840

49-
Prerequisites:
50-
- Install Darwin build tool:
51-
```
52-
curl -L https://github.com/OUIsolutions/Darwin/releases/download/0.020/darwin.out -o darwin.out && chmod +x darwin.out && sudo mv darwin.out /usr/bin/darwin
53-
```
41+
Using Docker (default):
42+
```bash
43+
darwin run_blueprint build/ --mode folder build_release
44+
```
5445

55-
Build steps:
56-
1. Clone the repository
57-
2. Navigate to the project directory
58-
3. Run:
59-
```
60-
darwin run_blueprint build/ --mode folder build_release
61-
```
46+
Using Podman:
47+
```bash
48+
darwin run_blueprint build/ --mode folder build_release --provider podman
49+
```
6250

63-
## 📚 Quick Start Guide
6451

65-
### Making a Simple GET Request
52+
## Basic Usage
6653

54+
### Simple GET Request
6755
```lua
68-
-- Import the library
69-
local luabear = require("luaBear.luaBear")
56+
local luabear = require("luaBear.luaBear")
57+
local response = luabear.fetch({url="https://example.com/"})
7058

71-
-- Make a simple GET request
72-
local response = luabear.fetch({url = "https://example.com/"})
73-
74-
-- Check status code
75-
print("Status code:", response.status_code)
76-
77-
-- Print response headers
59+
print("status code:", response.status_code)
7860
for header_name, header_value in pairs(response.headers) do
79-
print(header_name .. ": " .. header_value)
61+
print(header_name .. ":" .. header_value)
8062
end
81-
82-
-- Print response body
8363
print(response.read_body())
8464
```
8565

86-
## 📖 Common Examples
87-
88-
### Reading a JSON Response
89-
66+
### Reading Response in Chunks
9067
```lua
91-
local luabear = require("luaBear.luaBear")
92-
local response = luabear.fetch({url = "https://jsonplaceholder.typicode.com/todos/1"})
93-
local data = response.read_body_json()
94-
95-
-- Access JSON data easily
96-
print("User ID:", data.userId)
97-
print("Title:", data.title)
98-
print("Completed:", data.completed)
68+
local luabear = require("luaBear.luaBear")
69+
local response = luabear.fetch({url="https://example.com/"})
70+
while true do
71+
local chunk = response.read_body_chunk(1024)
72+
if not chunk then break end
73+
print(chunk)
74+
end
9975
```
10076

101-
### Sending Form Data (POST)
77+
### Reading JSON Response
78+
```lua
79+
local luabear = require("luaBear.luaBear")
80+
local response = luabear.fetch({url="https://jsonplaceholder.typicode.com/todos/1"})
81+
local body = response.read_body_json()
82+
print(body["userId"], body["title"], body["completed"])
83+
```
10284

85+
### Sending String Data (POST)
10386
```lua
104-
local luabear = require("luaBear.luaBear")
87+
local luabear = require("luaBear.luaBear")
10588
local response = luabear.fetch({
106-
url = "https://httpbin.org/post",
107-
method = "POST",
108-
body = {
109-
username = "bearuser",
110-
password = "bearpassword"
111-
}
89+
url="http://localhost:3001/",
90+
method="POST",
91+
body="Hello, LuaBear!",
11292
})
11393
print(response.read_body())
11494
```
11595

116-
### Uploading a File
117-
96+
### Sending JSON Data (POST)
11897
```lua
119-
local luabear = require("luaBear.luaBear")
98+
local luabear = require("luaBear.luaBear")
99+
luabear.nil_code = "nil" -- set the nil code to avoid errors with nil values
120100
local response = luabear.fetch({
121-
url = "https://httpbin.org/post",
122-
method = "POST",
123-
body = luabear.file_stream("test.png")
101+
url="http://localhost:3001/",
102+
method="POST",
103+
body={username="LuaBear", password="secret", email="nil"},
124104
})
125105
print(response.read_body())
126106
```
127107

128-
## 📝 API Reference
129-
130-
### Main Functions
131-
132-
#### `luabear.fetch(options)`
133-
Makes an HTTP request with the given options.
134-
135-
**Parameters:**
136-
- `options.url` (string): Target URL (required)
137-
- `options.method` (string): HTTP method like "GET", "POST" (default: "GET")
138-
- `options.body` (string/table/stream): Request body data
139-
- `options.headers` (table): Custom HTTP headers
140-
141-
**Returns:**
142-
A response object with:
143-
- `status_code`: HTTP status code (e.g., 200, 404)
144-
- `headers`: Table of response headers
145-
- `read_body()`: Returns full response as string
146-
- `read_body_chunk(size)`: Returns response in chunks
147-
- `read_body_json()`: Parses and returns JSON response as table
148-
149-
#### `luabear.file_stream(filename)`
150-
Creates a file stream for uploading files.
151-
152-
**Parameters:**
153-
- `filename` (string): Path to the file
154-
155-
**Returns:**
156-
A file stream object to be used with `fetch()`
157-
158-
## 🧪 More Examples
159-
160-
Check the `examples/` directory for complete working examples:
161-
- Basic requests: `read_all.lua`
162-
- Chunked reading: `read_chunk.lua`
163-
- JSON handling: `read_json.lua`, `sending_json.lua`
164-
- Binary data: `sending_binary.lua`
165-
- File uploads: `sending_file.lua`
166-
- Custom headers: `setting_headers.lua`
167-
168-
## 📄 License
108+
### Sending Binary Data (POST)
109+
```lua
110+
local luabear = require("luaBear.luaBear")
111+
local response = luabear.fetch({
112+
url="http://localhost:3000/",
113+
method="POST",
114+
body=io.open("test.png", "rb"):read("*a"),
115+
})
116+
print(response.read_body())
117+
```
169118

170-
This project is licensed under the MIT License - see the [LICENSE](/LICENSE) file for details.
119+
### Sending File Stream (POST)
120+
```lua
121+
local luabear = require("luaBear.luaBear")
122+
local response = luabear.fetch({
123+
url="http://localhost:3000/",
124+
method="POST",
125+
body=luabear.file_stream("test.png")
126+
})
127+
print(response.read_body())
128+
```
171129

172-
## 🤝 Contributing
130+
### Setting Custom Headers
131+
```lua
132+
local luabear = require("luaBear.luaBear")
133+
local response = luabear.fetch({
134+
url="http://localhost:3001/",
135+
method="POST",
136+
headers={
137+
username="your username",
138+
password="your password"
139+
}
140+
})
141+
print(response.read_body())
142+
```
173143

174-
Contributions are welcome! Feel free to open issues or submit pull requests.
144+
## API Reference
145+
146+
### `luabear.fetch(options)`
147+
Performs an HTTP request.
148+
- `options.url` (string): The URL to request (required)
149+
- `options.method` (string): HTTP method (default: "GET")
150+
- `options.body` (string/table/stream): Request body (optional)
151+
- `options.headers` (table): Custom headers (optional)
152+
153+
Returns a `response` object:
154+
- `response.status_code`: HTTP status code
155+
- `response.headers`: Table of response headers
156+
- `response.read_body()`: Reads the full response body as a string
157+
- `response.read_body_chunk(size)`: Reads the response body in chunks of `size` bytes
158+
- `response.read_body_json()`: Parses the response body as JSON and returns a Lua table
159+
160+
### `luabear.file_stream(filename)`
161+
Returns a file stream object for use as a request body (for large/binary files).
162+
163+
### `luabear.nil_code`
164+
Set this to a string to represent `nil` values in JSON encoding (useful for APIs that require explicit nulls).
165+
166+
## Examples
167+
See the `examples/` directory for more usage examples:
168+
- `read_all.lua`: Basic GET request and print all response
169+
- `read_chunk.lua`: Read response in chunks
170+
- `read_json.lua`: Parse JSON response
171+
- `sending_str.lua`: Send string data
172+
- `sending_json.lua`: Send JSON data
173+
- `sending_binary.lua`: Send binary data
174+
- `sending_file.lua`: Send file stream
175+
- `setting_headers.lua`: Custom headers
176+
177+
## License
178+
See [LICENSE](/LICENSE).

0 commit comments

Comments
 (0)