一键上传并创建任务(整合接口)
POST /api/openapi/uploadAndCreateTask
功能说明
- 将「上传文件」和「创建任务」两步合并为一步
- 简化调用流程,减少网络请求次数
- 适合需要快速创建任务的场景
请求参数
| 参数 | 位置 | 类型 | 必填 | 说明 |
|---|---|---|---|---|
| file | form-data | File | 是 | 仅 .txt,≤ 1 GB,≤ 100 万行 |
| taskType | form-data | String | 是 | 业务类型,见任务类型列表 |
| description | form-data | String | 是 | 任务描述(通常为原始文件名) |
| countryCode | form-data | String | 否 | 国家代码(如:US) |
| countryAreaCode | form-data | String | 否 | 区域代码(如:1) |
返回示例
{
"code": 200,
"msg": "文件上传并创建任务成功",
"data": "TASK-20250120110901-1001"
}
返回说明:
data字段直接返回任务 ID(字符串格式)- 可以立即使用返回的 taskId 查询任务状态
优势
- ✅ 一次请求完成两步操作
- ✅ 减少网络延迟和请求次数
- ✅ 简化代码逻辑
- ✅ 自动处理文件上传和任务创建的关联
错误返回示例
{
"code": 500,
"msg": "文件上传失败:文件解析失败或文件内容为空"
}
使用建议
推荐使用此整合接口,可以简化调用流程。如需分步查看上传结果和预估费用,可使用原有的分步接口。
调用示例(Java)
// 1. 生成签名
String timestamp = String.valueOf(System.currentTimeMillis());
String data = account + ":" + timestamp;
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(new SecretKeySpec(apiKey.getBytes(), "HmacSHA256"));
String signature = Base64.getEncoder().encodeToString(mac.doFinal(data.getBytes()));
String headerKey = timestamp + "." + signature;
// 2. 构建请求
HttpClient client = HttpClient.newHttpClient();
MultipartBodyPublisher publisher = new MultipartBodyPublisher()
.addPart("file", Paths.get("data.txt"))
.addPart("taskType", "wsExist")
.addPart("description", "data.txt")
.addPart("countryCode", "US")
.addPart("countryAreaCode", "1");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.ts-filter.com/api/openapi/uploadAndCreateTask"))
.header("X-Api-Account", account)
.header("X-Api-Key", headerKey)
.POST(publisher.build())
.build();
// 3. 发送请求 并获取任务ID
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONObject json = new JSONObject(response.body());
String taskId = json.getString("data");
System.out.println("任务创建成功,任务ID: " + taskId);
调用示例(Python)
import requests
import hmac
import hashlib
import base64
import time
# 1. 生成签名
timestamp = str(int(time.time() * 1000))
data = f"{account}:{timestamp}"
signature = base64.b64encode(
hmac.new(api_key.encode(), data.encode(), hashlib.sha256).digest()
).decode()
header_key = f"{timestamp}.{signature}"
# 2. 发送请求
files = {'file': open('data.txt', 'rb')}
data = {
'taskType': 'wsExist',
'description': 'data.txt',
'countryCode': 'US',
'countryAreaCode': '1'
}
headers = {
'X-Api-Account': account,
'X-Api-Key': header_key
}
response = requests.post(
'https://api.ts-filter.com/api/openapi/uploadAndCreateTask',
files=files,
data=data,
headers=headers
)
# 3. 获取任务ID
result = response.json()
task_id = result['data']
print(f"任务创建成功,任务ID: {task_id}")