@@ -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
169188class ApiClient {
@@ -211,7 +230,7 @@ const api = new ApiClient('https://api.example.com', 'token123');
211230const response = api .get (' /users/1' );
212231```
213232
214- ### 示例 7 : 重试机制
233+ ### 示例 8 : 重试机制
215234
216235``` javascript
217236function requestWithRetry (url , options , maxRetries = 3 ) {
0 commit comments