网络请求
网络请求处理器用于发送各种 HTTP 请求,支持 GET、POST、PUT、PATCH、DELETE、HEAD 等方法。
处理器列表
处理器 | 说明 | 参数格式 |
---|---|---|
http.Get | 发送 HTTP GET 请求 | [<URL>, <Query (可选)>, <Headers (可选)>] |
http.Post | 发送 HTTP POST 请求 | [<URL>, <Payload (可选)>, <Files (可选)>, <Query(可选)>, <Headers (可选)>] |
http.PostJSON | 发送 HTTP POST 请求,payload 为 JSON 格式数据 | [<URL>, <Payload>, <Headers (可选)>] |
http.Put | 发送 HTTP PUT 请求 | [<URL>, <Payload (可选)>, <Query(可选)>, <Headers (可选)>] |
http.PutJSON | 发送 HTTP PUT 请求,payload 为 JSON 格式数据 | [<URL>, <Payload>, <Headers (可选)>] |
http.Patch | 发送 HTTP PATCH 请求 | [<URL>, <Payload (可选)>, <Query(可选)>, <Headers (可选)>] |
http.Delete | 发送 HTTP DELETE 请求 | [<URL>, <Payload (可选)>, <Query(可选)>, <Headers (可选)>] |
http.Head | 发送 HTTP HEAD 请求 | [<URL>, <Payload (可选)>, <Query(可选)>, <Headers (可选)>] |
http.Send | 发送任意 HTTP 请求 | [<METHOD>, <URL>, <Payload (可选)>, <Query (可选)>, <Headers (可选)>, <Files(可选)>] |
http.Stream | 发送流式 HTTP 请求 | [<METHOD>, <URL>, <Handler process name>, <Payload (可选)>, <Query (可选)>, <Headers (可选)>] |
参数说明
- URL: 请求地址,字符串类型
- Query: 查询参数,支持以下格式:
- 对象格式:
Record<string, string>
- 键值对数组:
[string, string][]
- 对象数组:
Array<Record<string, string>>
- 字符串:
string
- 对象格式:
- Payload: 请求体数据,支持任意类型(any)
- Headers: 请求头,支持以下格式:
- 单个对象:
Record<string, string>
- 对象数组:
Record<string, string>[]
- 单个对象:
- Files: 上传文件,支持以下格式:
- 文件映射:
Record<string, string>
,key 为文件字段名,value 为文件路径 - 文件数组:
HttpFile[]
,用于 Send 方法的文件上传
- 文件映射:
- METHOD: HTTP 请求方法,如 GET、POST、PUT 等
- Handler process name: Stream 请求的处理器名称,回调函数需返回:
- 1: 继续接收数据
- 0: 停止接收
- -1: 发生异常
文件上传类型
typescript
interface UploadFile {
uid?: string; // 文件唯一标识(用于分片上传)
range?: string; // 分片范围,格式:bytes start-end/total
sync?: boolean; // 是否同步上传,默认false
name: string; // 文件名
tempFile: string; // 临时文件路径
size: number; // 文件大小
header: {
// 文件MIME头信息
[key: string]: string[];
};
error?: string; // 错误信息
}
返回值
所有 HTTP 请求处理器返回一个 HttpResponse 对象,包含以下字段:
typescript
interface HttpResponse {
status: number; // HTTP 状态码
headers: {
// 响应头
[key: string]: string[];
};
data: any; // 响应体
error?: string; // 错误信息,可选
}
// 文件上传响应
type UploadFileResponse =
| string
| {
path: string; // 文件路径或URL
progress?: {
// 上传进度信息
total: number; // 总字节数
uploaded: number; // 已上传字节数
completed: boolean; // 是否完成
};
uid?: string; // 文件唯一标识(用于分片上传)
[key: string]: any; // 其他额外信息
};
使用示例
发送 GET 请求
javascript
var response = Process('http.Get', url, params, {
'Content-Type': 'application/json',
Auth: 'token',
});
发送 POST 请求
javascript
let response = Process('http.PostJSON', url, data, {
'Content-Type': 'application/json',
Auth: 'token',
});
发送 PATCH 请求示例:http.Send
json
let response = Process("http.Send", "PATCH", url, {}, body, {
"Content-Type": "application/json",
"Auth": "token",
});
发送 DELETE 请求示例:http.Send
javascript
let response = Process('http.Send', 'DELETE', url, {}, body, {
'Content-Type': 'application/json',
Auth: 'token',
});
使用 http 对象发送 HTTP 请求,参数表与返回值与 http.* 处理器一致
发送流式请求
使用 http.Stream 处理器发送流式请求时,需要提供一个处理器函数来处理响应数据,在 handler 函数中,返回 1 表示继续接收数据,返回 0 表示停止接收,返回-1 表示异常。
javascript
let response = Process('http.Stream', 'POST', url, 'handler', payload, {
Accept: 'text/event-stream; charset=utf-8',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + token,
});
// 处理器函数示例
function handler(payload) {
// 处理接收到的数据
console.log(payload);
return 1; // 返回1表示继续接收数据,返回0表示停止接收,返回-1表示异常
}
约定
- 示例中约定应用根目录为
/data/app
, 实际编写时需替换为应用根目录。 - 使用
<>
标识自行替换的内容。 例如:icon-<图标名称>
, 实际编写时应替换为:icon-foo
,icon-bar
...
示例
System
/data/app/scripts/test.js
javascript
function HttpTest() {
// Get
http.Get('http://127.0.0.1/get?foo=bar');
http.Get('http://127.0.0.1/get?foo=bar', { hello: 'world' });
http.Get(
'http://127.0.0.1/get?foo=bar',
{ hello: 'world' },
{ Auth: 'Test' },
);
// Post
http.Post('http://127.0.0.1/path?foo=bar');
http.Post('http://127.0.0.1/path?foo=bar', { name: 'Lucy' });
http.Post('http://127.0.0.1/path?foo=bar', { name: 'Lucy' }, null, {
hello: 'world',
});
http.Post(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
null,
{ hello: 'world' },
{ Auth: 'Test' },
);
// Post File via payload 文件路径为相对路径 相对地址, 示例: `/text/foo.txt`, 绝对路径为: `/data/app/data/text/foo.txt`。
http.Post('http://127.0.0.1/path?foo=bar', '/path/root/file');
// Post File via files
http.Post(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ f1: '/path/root/f1', f2: '/path/root/f2' },
);
// Patch
http.Patch('http://127.0.0.1/path?foo=bar');
http.Patch('http://127.0.0.1/path?foo=bar', { name: 'Lucy' });
http.Patch(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
);
http.Patch(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
{ Auth: 'Test' },
);
// Put
http.Put('http://127.0.0.1/path?foo=bar');
http.Put('http://127.0.0.1/path?foo=bar', { name: 'Lucy' });
http.Put(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
);
http.Put(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
{ Auth: 'Test' },
);
// Delete
http.Delete('http://127.0.0.1/path?foo=bar');
http.Delete('http://127.0.0.1/path?foo=bar', { name: 'Lucy' });
http.Delete(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
);
http.Delete(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
{ Auth: 'Test' },
);
// Head
http.Head('http://127.0.0.1/path?foo=bar');
http.Head('http://127.0.0.1/path?foo=bar', { name: 'Lucy' });
http.Head(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
);
http.Head(
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
{ Auth: 'Test' },
);
// Send
http.Send('POST', 'http://127.0.0.1/path?foo=bar');
http.Send('POST', 'http://127.0.0.1/path?foo=bar', { name: 'Lucy' });
http.Send(
'POST',
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
);
http.Send(
'POST',
'http://127.0.0.1/path?foo=bar',
{ name: 'Lucy' },
{ hello: 'world' },
{ Auth: 'Test' },
);
}