Skip to content

Commit 663c46d

Browse files
🔄 Sync from private repository - 2026-03-25 02:07:16
1 parent a83968e commit 663c46d

3 files changed

Lines changed: 43 additions & 11 deletions

File tree

TrollScript-Private

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Subproject commit 94e76cc88efccde632c074088d6d7cdc1662cbe6
1+
Subproject commit 3f3063bc41031131e6b082d5fbcade16bbbed53d

templates/API/HTTP.md

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,43 @@ const download = http.download(
3333
### HTTP 请求方法
3434

3535
#### `http.get(url, options?)`
36-
发送 GET 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, data?, headers?, error? }`
36+
发送 GET 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, body?, data?, bodyBytes?, headers?, error? }`
3737

3838
#### `http.post(url, options?)`
39-
发送 POST 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, data?, headers?, error? }`
39+
发送 POST 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, body?, data?, bodyBytes?, headers?, error? }`
4040

4141
#### `http.put(url, options?)`
42-
发送 PUT 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, data?, headers?, error? }`
42+
发送 PUT 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, body?, data?, bodyBytes?, headers?, error? }`
4343

4444
#### `http.delete(url, options?)`
45-
发送 DELETE 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, data?, headers?, error? }`
45+
发送 DELETE 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, body?, data?, bodyBytes?, headers?, error? }`
4646

4747
#### `http.patch(url, options?)`
48-
发送 PATCH 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, data?, headers?, error? }`
48+
发送 PATCH 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, body?, data?, bodyBytes?, headers?, error? }`
4949

5050
#### `http.head(url, options?)`
51-
发送 HEAD 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, data?, headers?, error? }`
51+
发送 HEAD 请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, body?, data?, bodyBytes?, headers?, error? }`
5252

5353
#### `http.request(url, options)`
54-
发送自定义请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, data?, headers?, error? }`
54+
发送自定义请求。**参数:** `url` (string), `options` (object) **返回:** `{ success, status?, body?, data?, bodyBytes?, headers?, error? }`
5555

5656
**options 配置项:**
5757
- `method` - HTTP 方法(GET、POST 等)
5858
- `headers` - 请求头对象
5959
- `body` - 请求体(字符串)
6060
- `timeout` - 超时时间(秒,默认 30)
6161
- `insecure` - 忽略 SSL 证书验证(默认 false)
62+
- `encoding` - 响应体编码方式,支持 `utf8`(默认)和 `base64`
63+
- `includeBodyBytes` - 是否额外返回 `bodyBytes` 字节数组;二进制 `base64` 场景默认会返回
6264

6365
**返回值:**
6466
- `success` - 请求是否成功
6567
- `status` - HTTP 状态码
66-
- `data` - 响应内容(字符串)
68+
- `body` - 响应内容字符串
69+
- `data` - `body` 的兼容别名
70+
- `bodyBytes` - 原始响应字节数组;在 `encoding: 'base64'``includeBodyBytes: true` 时返回
71+
- `byteLength` - 原始响应体字节长度
72+
- `encoding` - 实际使用的响应编码
6773
- `headers` - 响应头对象
6874
- `error` - 错误信息(如果失败)
6975

@@ -163,7 +169,20 @@ if (response.success) {
163169
}
164170
```
165171

166-
### 示例 6: API 封装
172+
### 示例 6: 获取图片 Base64
173+
174+
```javascript
175+
const response = await http.get('https://httpbin.org/image/png', {
176+
encoding: 'base64'
177+
});
178+
179+
if (response.success) {
180+
console.log('图片 Base64 长度:', response.body.length);
181+
console.log('前 16 个字节:', response.bodyBytes.slice(0, 16));
182+
}
183+
```
184+
185+
### 示例 7: API 封装
167186

168187
```javascript
169188
class ApiClient {
@@ -211,7 +230,7 @@ const api = new ApiClient('https://api.example.com', 'token123');
211230
const response = api.get('/users/1');
212231
```
213232

214-
### 示例 7: 重试机制
233+
### 示例 8: 重试机制
215234

216235
```javascript
217236
function requestWithRetry(url, options, maxRetries = 3) {

templates/TestScripts/test_http.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ async function run() {
4242
assert(false, "POST request failed");
4343
}
4444

45+
console.log("Testing binary/base64 response...");
46+
try {
47+
const res = await http.get("https://httpbin.org/image/png", {
48+
encoding: "base64"
49+
});
50+
assert(typeof res.body === "string" && res.body.length > 0, "Base64 body returned");
51+
assert(Array.isArray(res.bodyBytes) && res.bodyBytes.length > 0, "bodyBytes returned");
52+
assert(res.data === res.body, "data alias matches body");
53+
} catch (e) {
54+
console.error("Binary Error:", e);
55+
assert(false, "Binary request failed");
56+
}
57+
4558
console.log("--- HTTP Module Test Finished ---");
4659
}
4760

0 commit comments

Comments
 (0)