HttpClient HTTP 客户端 API 文档
EasyBot 提供了一套完整的 HTTP 客户端 API,用于发送各种类型的 HTTP 请求。支持常见的 HTTP 方法,并提供了灵活的头部管理和响应处理功能。
核心接口
HeaderCollection 接口
HTTP 头部集合管理接口:
interface HeaderCollection {
[key: string]: string; // 索引器访问
Set(key: string, value: string): void;
Get(key: string): string;
Contains(key: string): boolean;
Remove(key: string): boolean;
Clear(): void;
GetKeys(): string[];
GetValues(): string[];
}
RequestMessage 接口
HTTP 请求消息结构:
interface RequestMessage {
url: string; // 请求URL
method: string; // HTTP方法
headers: HeaderCollection; // 请求头部
body?: string; // 请求体(可选)
}
ResponseMessage 接口
HTTP 响应消息结构:
interface ResponseMessage {
status: number; // 状态码
headers: HeaderCollection; // 响应头部
body: string; // 响应体
data?: any; // JSON反序列化对象(可选)
}
HeaderCollection 方法
Set()
功能: 设置 HTTP 头部值
语法: Set(key: string, value: string): void
示例:
headers.Set("Content-Type", "application/json");
Get()
功能: 获取 HTTP 头部值
语法: Get(key: string): string
示例:
const contentType = headers.Get("Content-Type");
Contains()
功能: 检查是否包含指定头部
语法: Contains(key: string): boolean
示例:
if (headers.Contains("Authorization")) {
logger.info("包含认证头部");
}
Remove()
功能: 移除指定头部
语法: Remove(key: string): boolean
示例:
const removed = headers.Remove("X-Custom-Header");
Clear()
功能: 清空所有头部
语法: Clear(): void
GetKeys()
功能: 获取所有头部名称
语法: GetKeys(): string[]
GetValues()
功能: 获取所有头部值
语法: GetValues(): string[]
Network 命名空间方法
request()
功能: 发送自定义 HTTP 请求
语法: function request(message: RequestMessage): Promise<ResponseMessage>
示例:
const requestMsg = {
url: "https://api.example.com/data",
method: "POST",
headers: headers,
body: JSON.stringify({ name: "test" })
};
const response = await network.request(requestMsg);
get()
功能: 发送 GET 请求
语法: function get(url: string): Promise<ResponseMessage>
示例:
const response = await network.get("https://api.example.com/users");
post()
功能: 发送 POST 请求
语法: function post(url: string, body: any): Promise<ResponseMessage>
示例:
const response = await network.post("https://api.example.com/users", { name: "Steve" });
put()
功能: 发送 PUT 请求
语法: function put(url: string, body: any): Promise<ResponseMessage>
示例:
const response = await network.put("https://api.example.com/users/123", { level: 30 });
del()
功能: 发送 DELETE 请求
语法: function del(url: string): Promise<ResponseMessage>
示例:
const response = await network.del("https://api.example.com/users/123");
head()
功能: 发送 HEAD 请求
语法: function head(url: string): Promise<ResponseMessage>
示例:
const response = await network.head("https://api.example.com/users/123");
options()
功能: 发送 OPTIONS 请求
语法: function options(url: string): Promise<ResponseMessage>
示例:
const response = await network.options("https://api.example.com/users");
使用示例
基础请求
// GET 请求
const response = await network.get("https://api.example.com/data");
if (response.status === 200) {
logger.info("数据:", response.data);
}
// POST 请求
const postData = { name: "Steve", level: 25 };
const result = await network.post("https://api.example.com/users", postData);
带认证的请求
const headers = {} as HeaderCollection;
headers.Set("Authorization", "Bearer token123");
headers.Set("Content-Type", "application/json");
const request = {
url: "https://api.example.com/protected",
method: "GET",
headers: headers
};
const response = await network.request(request);
错误处理
try {
const response = await network.get("https://api.example.com/data");
switch (response.status) {
case 200:
logger.info("成功:", response.data);
break;
case 404:
logger.info("资源未找到");
break;
default:
logger.info("状态码:", response.status);
}
} catch (error) {
logger.error("请求失败:", error);
}