flipped-aurora/gin-vue-admin · error
创建上游请求失败: %w
Error message
创建上游请求失败: %w
What it means
http.NewRequestWithContext rejected the request construction; the error is wrapped as '创建上游请求失败'. With an already-parsed URL this is rare and usually means an invalid HTTP method or a malformed URL string after requestURL.String().
Source
Thrown at server/mcp/http_client.go:191
if len(query) > 0 {
requestURL.RawQuery = query.Encode()
}
var reader io.Reader
if body != nil {
payload, err := json.Marshal(body)
if err != nil {
return nil, fmt.Errorf("序列化上游请求失败: %w", err)
}
reader = bytes.NewReader(payload)
}
timeoutCtx, cancel := context.WithTimeout(ctx, requestTimeout())
defer cancel()
req, err := http.NewRequestWithContext(timeoutCtx, method, requestURL.String(), reader)
if err != nil {
return nil, fmt.Errorf("创建上游请求失败: %w", err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set(upstreamAuthHeader, token)
if body != nil {
req.Header.Set("Content-Type", "application/json")
}
// 链路传播:外部 AI → MCP 进程 → GVA 主服务串成同一条 trace
logger.InjectTraceHeaders(timeoutCtx, req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, fmt.Errorf("请求上游服务失败: %w", err)
}
defer resp.Body.Close()
rawBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("读取上游响应失败: %w", err)View on GitHub (pinned to 3136500ef3)
Solutions
- Check the wrapped error text — it states exactly what NewRequestWithContext rejected
- Verify the HTTP method is a valid token (GET/POST/DELETE) and never empty
- Print requestURL.String() before creating the request to inspect the final URL
- Restrict methods to the getUpstream/postUpstream/deleteUpstream wrappers instead of calling doUpstream with arbitrary strings
Example fix
// before
method := "" // set somewhere upstream
req, err := http.NewRequestWithContext(timeoutCtx, method, requestURL.String(), reader)
// after
if method == "" {
method = http.MethodGet
}
req, err := http.NewRequestWithContext(timeoutCtx, method, requestURL.String(), reader) Defensive patterns
Strategy: validation
Validate before calling
func validMethod(m string) bool {
switch m {
case http.MethodGet, http.MethodPost, http.MethodDelete, http.MethodPut:
return true
}
return false
} Try / catch
result, err := doUpstream[Data](ctx, method, endpoint, nil, nil)
if err != nil {
if strings.Contains(err.Error(), "创建上游请求失败") {
// check method string and final URL before retrying
}
return err
} Prevention
- Use the getUpstream/postUpstream/deleteUpstream wrappers instead of raw doUpstream calls
- Never pass user-controlled strings as the HTTP method
- Log requestURL.String() in development builds for easy inspection
- Keep endpoint sanitization (trim, prefix '/') intact
When it happens
Trigger: Method variable is empty or contains invalid characters; the parsed URL became invalid when re-serialized; nil/incorrect reader combination; context already canceled does not fail here but at Do.
Common situations: Refactor left method as "" or lowercase typos passed through; endpoint injection produced a URL with spaces; building requests with a custom method string from user input.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/8e1ec1641970a447.
Report an issue: GitHub.