siyuan-note/siyuan · error
read body failed:
Error message
read body failed:
What it means
After passing the size precheck, HTTPRequest reads the response body with io.ReadAll(io.LimitReader(...)). This error wraps any failure during that read — typically a mid-stream network drop, a timeout while streaming, or decompression/transfer-encoding errors — with the underlying message appended after "read body failed: ".
Solutions
- Read the appended underlying error to distinguish timeout vs connection reset vs decompression failure
- Retry the request — transient network interruptions are the most common cause
- Increase relevant timeouts if the response is legitimately large and slow to stream
- Check intermediary proxies/CDNs for truncation and verify server stability
- Ensure the caller's context is not being cancelled prematurely
Example fix
// before
ctx := context.Background() // no deadline, but network drops mid-body
// after — retry on transient read failure
for i := 0; i < 3; i++ {
_, _, out, err := util.HTTPRequest(ctx, url)
if err == nil || !strings.HasPrefix(err.Error(), "read body failed:") { break }
time.Sleep(time.Second)
} Defensive patterns
Strategy: retry
Validate before calling
if !endpointHealthy(target) { return fmt.Errorf("endpoint unstable, skipping") } Try / catch
if err != nil && strings.HasPrefix(err.Error(), "read body failed:") {
time.Sleep(backoff)
// retry up to N times; surface a friendly message on exhaustion
} Prevention
- Retry transient stream errors with exponential backoff
- Use stable networks or increase timeouts for slow links
- Avoid fetching through unreliable proxies
When it happens
Trigger: Calling HTTPRequest when the connection is reset or times out while the body is streaming; the server closes the connection prematurely; a proxy/CDN truncates the transfer; gzip/deflate body corruption; context cancellation mid-read.
Common situations: Flaky mobile/VPN networks; servers with aggressive idle timeouts on slow links; overloaded endpoints aborting large-ish responses; intermediaries (nginx, Cloudflare) cutting the stream; laptop sleep during a fetch.
Understand the failure class
Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.
Related errors
- download failed:
- read body failed
- request failed:
- web search read response failed
- access to private/internal IP is prohibited
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/b589bd89da2342f0.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/util/httprequest.go:356
return 0, "", "", errors.New("nil response")
}
defer resp.Body.Close()
statusCode = resp.StatusCode
contentType = resp.Header.Get("Content-Type")
maxReadBytes := int64(maxHTTPRequestBytes)
if !isTextContentType(contentType) {
maxReadBytes = maxHTTPRequestFileBytes
}
// ContentLength 为 -1(chunked)时跳过大小预检,交由 LimitReader 兜底截断。
if resp.ContentLength > maxReadBytes {
return statusCode, contentType, "", errors.New("response too large")
}
respBody, rerr := io.ReadAll(io.LimitReader(resp.Body, maxReadBytes))
if rerr != nil {
return statusCode, contentType, "", errors.New("read body failed: " + rerr.Error())
}
// 二进制响应落盘,返回文件路径,供智能体按需进一步处理。
if !isTextContentType(contentType) {
importDir := filepath.Join(TempDir, "import")
if merr := os.MkdirAll(importDir, 0755); merr != nil {
return statusCode, contentType, "", errors.New("create import dir failed: " + merr.Error())
}
filename := extractFilename(rawURL, contentType)
filePath := filepath.Join(importDir, filename)
if werr := os.WriteFile(filePath, respBody, 0644); werr != nil {
return statusCode, contentType, "", errors.New("write file failed: " + werr.Error())
}
return statusCode, contentType, fmt.Sprintf("Saved to: %s (%d bytes)", filePath, len(respBody)), nil
}
return statusCode, contentType, truncateRunes(string(respBody), maxHTTPRequestChars), nil
}View on GitHub (pinned to 9f775e8a12)