MCPcopy Create free account
hub / github.com/ZyphrZero/z.ai2api_python / get_dynamic_headers

Function get_dynamic_headers

app/core/upstream.py:78–146  ·  view source on GitHub ↗

生成上游请求所需的动态浏览器 headers。

(
    chat_id: str = "",
    browser_type: Optional[str] = None,
)

Source from the content-addressed store, hash-verified

76 return str(uuid.uuid4())
77
78def get_dynamic_headers(
79 chat_id: str = "",
80 browser_type: Optional[str] = None,
81) -> Dict[str, str]:
82 """生成上游请求所需的动态浏览器 headers。"""
83 browser_choices = [
84 "chrome",
85 "chrome",
86 "chrome",
87 "edge",
88 "edge",
89 "firefox",
90 "safari",
91 ]
92 selected_browser = browser_type or random.choice(browser_choices)
93 user_agent = get_random_user_agent(selected_browser)
94 fe_version = get_latest_fe_version()
95
96 chrome_version = "139"
97 edge_version = "139"
98
99 if "Chrome/" in user_agent:
100 try:
101 chrome_version = user_agent.split("Chrome/")[1].split(".")[0]
102 except Exception:
103 pass
104
105 if "Edg/" in user_agent:
106 try:
107 edge_version = user_agent.split("Edg/")[1].split(".")[0]
108 sec_ch_ua = (
109 f'"Microsoft Edge";v="{edge_version}", '
110 f'"Chromium";v="{chrome_version}", "Not_A Brand";v="24"'
111 )
112 except Exception:
113 sec_ch_ua = (
114 f'"Not_A Brand";v="8", "Chromium";v="{chrome_version}", '
115 f'"Google Chrome";v="{chrome_version}"'
116 )
117 elif "Firefox/" in user_agent:
118 sec_ch_ua = None
119 else:
120 sec_ch_ua = (
121 f'"Not_A Brand";v="8", "Chromium";v="{chrome_version}", '
122 f'"Google Chrome";v="{chrome_version}"'
123 )
124
125 headers = {
126 "Content-Type": "application/json",
127 "Accept": "application/json, text/event-stream",
128 "Connection": "keep-alive",
129 "Cache-Control": "no-cache",
130 "User-Agent": user_agent,
131 "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
132 "X-FE-Version": fe_version,
133 "Origin": "https://chat.z.ai",
134 }
135

Callers 2

transform_requestMethod · 0.70

Calls 2

get_random_user_agentFunction · 0.90
get_latest_fe_versionFunction · 0.90

Tested by

no test coverage detected