{"id":"45f1d3a798467783","repo":"gohugoio/hugo","slug":"failed-to-read-remote-resource-q-w","errorCode":null,"errorMessage":"failed to read remote resource %q: %w","messagePattern":"failed to read remote resource %q: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"resources/resource_factories/create/remote.go","lineNumber":298,"sourceCode":"\t\t\t// To cache this, we need to make sure the body is read.\n\t\t\tio.Copy(io.Discard, res.Body)\n\t\t\treturn nil, nil\n\t\t}\n\n\t\tif res.StatusCode < 200 || res.StatusCode > 299 {\n\t\t\treturn nil, toHTTPError(fmt.Errorf(\"failed to fetch remote resource from '%s': %s\", uri, http.StatusText(res.StatusCode)), res, !isHeadMethod, options.ResponseHeaders)\n\t\t}\n\n\t\tvar (\n\t\t\tbody      []byte\n\t\t\tmediaType media.Type\n\t\t)\n\t\t// A response to a HEAD method should not have a body. If it has one anyway, that body must be ignored.\n\t\t// See https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/HEAD\n\t\tif !isHeadMethod && res.Body != nil {\n\t\t\tbody, err = io.ReadAll(res.Body)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to read remote resource %q: %w\", uri, err)\n\t\t\t}\n\t\t}\n\n\t\tfilename := path.Base(rURL.Path)\n\t\tif _, params, _ := mime.ParseMediaType(res.Header.Get(\"Content-Disposition\")); params != nil {\n\t\t\tif _, ok := params[\"filename\"]; ok {\n\t\t\t\tfilename = params[\"filename\"]\n\t\t\t}\n\t\t}\n\n\t\tcontentType := res.Header.Get(\"Content-Type\")\n\n\t\t// For HEAD requests we have no body to work with, so we need to use the Content-Type header.\n\t\tif isHeadMethod || c.rs.ExecHelper.Sec().HTTP.MediaTypes.Accept(contentType) {\n\t\t\tvar found bool\n\t\t\tmediaType, found = c.rs.MediaTypes().GetByType(contentType)\n\t\t\tif !found {\n\t\t\t\t// A media type not configured in Hugo, just create one from the content type string.","sourceCodeStart":280,"sourceCodeEnd":316,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/resources/resource_factories/create/remote.go#L280-L316","documentation":"The HTTP request succeeded with a 2xx status, but reading the response body with `io.ReadAll` failed partway through. This indicates the connection was interrupted mid-transfer — the server closed early, a timeout expired during the read, or the response was truncated relative to its Content-Length.","triggerScenarios":"`resources.GetRemote` downloading a large file when the per-request `timeout` (or global timeout) expires during body read (context deadline exceeded), the remote server or a proxy resetting the connection mid-stream, or gzip/chunked-encoding corruption from the origin.","commonSituations":"Fetching large assets (images, archives, big JSON) with the default or a short `timeout` on slow CI networks, flaky CDNs or proxies dropping long transfers, and corporate proxies interfering with chunked responses.","solutions":["Increase the timeout for that request: `dict \"timeout\" \"120s\"` (per-request) or raise `timeout` in site config.","Retry the build — if it's transient network flakiness, the file cache will keep the successful result; consider a stable `key` option to control cache eviction.","Test the download from the build environment with `curl -o /dev/null -w '%{size_download}' <url>` to confirm the server serves the full body reliably; switch to a more reliable mirror/CDN if not."],"exampleFix":"{{/* before */}}\n{{ $r := resources.GetRemote $bigFileURL }}\n{{/* after */}}\n{{ $r := resources.GetRemote $bigFileURL (dict \"timeout\" \"120s\") }}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"{{ $r := resources.GetRemote $url }}\n{{ with $r.Err }}\n  {{ errorf \"remote resource body read failed for %s: %s\" $url . }}\n{{ end }}","preventionTips":["Set a generous timeout for large remote files — body reads that outlive the deadline fail here even after a 200 status","Prefer stable CDN URLs over origin servers that may truncate or reset long transfers","Check .Err before using .Content/.RelPermalink; a fetch can start OK and still fail mid-body"],"tags":["hugo","get-remote","network","timeout","io"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}