Skip to content

Commit 422d266

Browse files
author
mobiusy
committed
feat(请求头): 新增可选 headers 参数支持自定义请求头
实现工具代码读取 JSON 字符串格式的 headers 参数,并与默认 SSE 请求头合并,支持用户自定义请求头覆盖默认值
1 parent e3409fa commit 422d266

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

http_request_stream/tools/http_request_stream.py

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,47 @@ def _invoke(self, tool_parameters: dict[str, Any]) -> Generator[ToolInvokeMessag
2020
url = tool_parameters.get("url")
2121
method = tool_parameters.get("method", "GET")
2222
body = tool_parameters.get("body", None)
23+
headers_str = tool_parameters.get("headers", None)
2324
if not url:
2425
raise httpx.InvalidURL("URL cannot be empty.")
2526

2627
if not url.startswith(("http://", "https://")):
2728
raise httpx.InvalidURL("URL must start with http:// or https://.")
2829

30+
# 解析 headers 参数:支持用户以 JSON 对象字符串方式传入请求头
31+
user_headers: dict[str, Any] = {}
32+
if headers_str:
33+
try:
34+
parsed = loads(headers_str)
35+
if isinstance(parsed, dict):
36+
user_headers = parsed
37+
else:
38+
raise ValueError("headers must be a JSON object string.")
39+
except ValueError:
40+
# 用户传入的 headers 字符串不是合法 JSON 或不是对象
41+
raise ValueError("headers must be a valid JSON object string.")
42+
2943
# 构造 httpx.stream 的参数,增加 SSE 兼容头部与超时设置
3044
stream_kwargs: dict[str, Any] = {
3145
"method": method,
3246
"url": url,
33-
# SSE 推荐的请求头,提升与代理/网关的兼容性
34-
"headers": {
35-
"Accept": "text/event-stream",
36-
"Cache-Control": "no-cache",
37-
"Connection": "keep-alive",
38-
},
3947
# 读取不设整体超时,连接阶段限制为 5 秒,避免读超时中断流
4048
"timeout": httpx.Timeout(timeout=None, connect=5.0),
4149
}
4250

51+
# 默认 SSE 兼容请求头,提升与代理/网关的兼容性
52+
default_headers: dict[str, str] = {
53+
"Accept": "text/event-stream",
54+
"Cache-Control": "no-cache",
55+
"Connection": "keep-alive",
56+
}
57+
# 合并用户请求头,用户值覆盖默认值,确保所有值为字符串类型
58+
merged_headers: dict[str, str] = {
59+
**default_headers,
60+
**{str(k): str(v) for k, v in user_headers.items()}
61+
}
62+
stream_kwargs["headers"] = merged_headers
63+
4364
if body:
4465
# 检查body是否是合法的json格式
4566
try:

http_request_stream/tools/http_request_stream.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ parameters:
7575
pt_BR: request body
7676
llm_description: 请求主体
7777
form: llm
78+
- name: headers
79+
type: string
80+
required: false
81+
label:
82+
en_US: Headers
83+
zh_Hans: 请求头
84+
human_description:
85+
en_US: "Optional request headers in JSON object string, e.g. {\"Authorization\": \"Bearer xxx\", \"X-Custom\": \"value\"}."
86+
zh_Hans: "可选的请求头,使用 JSON 对象字符串,例如 {\"Authorization\": \"Bearer xxx\", \"X-Custom\": \"value\"}。"
87+
llm_description: "Optional request headers as a JSON object string; when provided, they will be merged with default SSE headers."
88+
form: llm
7889

7990
extra:
8091
python:

http_request_stream/working/progress.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99

1010
## 已完成工作
1111
- [2025-11-07 16:53:37 +08:00] 完成 README 文档的全面撰写与结构化说明
12+
- [2025-11-07 17:51:36 +08:00] 新增可选 headers 参数(JSON 对象字符串),实现工具代码读取并与默认 SSE 请求头合并,支持自定义请求头覆盖默认值
1213

1314
## 待办事项
14-
- [ ] 支持自定义请求头与认证信息(如 Bearer Token)
15+
- [x] 支持自定义请求头(headers 参数,JSON 字符串)
16+
- [ ] 提供认证信息示例(如 Bearer Token)
1517
- [ ] 增加更多使用示例(不同服务的 SSE 与分块响应)
1618
- [ ] 完善错误信息的指引与用户友好提示
1719
- [ ] 在 GUIDE.md 中补充工作流配置的图示与步骤

0 commit comments

Comments
 (0)