{"record":{"id":"15807aca75980d28","repo":"OpenNHP/opennhp","slug":"failed-to-download-file-s-status-code-s","errorCode":null,"errorMessage":"failed to download file (%s): status code %s","messagePattern":"failed to download file \\((.+?)\\): status code (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"nhp/utils/utils.go","lineNumber":86,"sourceCode":"\t}\n\n\tfileName := filepath.Base(fileUrl)\n\ttempFilePath := filepath.Join(tempDir, fileName)\n\n\toutFile, err := os.Create(tempFilePath)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tdefer outFile.Close()\n\n\tresp, err := http.Get(fileUrl) //nolint:gosec // G107: URL comes from trusted configuration\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode != http.StatusOK {\n\t\treturn \"\", fmt.Errorf(\"failed to download file (%s): status code %s\", fileUrl, resp.Status)\n\t}\n\n\t_, err = io.Copy(outFile, resp.Body)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\treturn tempFilePath, nil\n}\n\nfunc GenerateTempFilePath(pattern string) (string, error) {\n\tfile, err := os.CreateTemp(\"\", pattern)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\ttempPath := file.Name()\n","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/OpenNHP/opennhp/blob/6e04ca5ff03222a699c24205cd4bf8fee9af7ffe/nhp/utils/utils.go#L68-L104","documentation":"DownloadFileToTemp in nhp/utils/utils.go performs an HTTP GET and requires HTTP 200. Any other status (404, 403, 500, etc.) aborts the download and returns this formatted error containing the URL and the response status string. The body is not consumed; the transfer is treated as failed.","triggerScenarios":"Calling DownloadFileToTemp(fileUrl, ...) where the server responds non-200: attestation/policy/data URLs that are stale, expired signed URLs (403), moved endpoints (404), or server-side errors (5xx).","commonSituations":"Expired pre-signed S3 URLs used by RefreshDataAccess/GetPolicy; wrong base URL or port in config; behind a proxy returning 407; service temporarily down (503) during deployments.","solutions":["Check the embedded status code and URL — verify the file exists at that URL (curl -I)","Renew/refresh credentials or signed URLs if 403/401","Add retry with backoff for transient 5xx responses","Fix the configured base URL/path if 404"],"exampleFix":"// before\npath, err := DownloadFileToTemp(expiredSignedURL, dir)\n// after\nurl := refreshSignedURL(key) // regenerate pre-signed URL before each fetch\npath, err := DownloadFileToTemp(url, dir)","handlingStrategy":"retry","validationCode":"u, err := url.Parse(fileUrl); if err != nil || u.Scheme == \"\" || u.Host == \"\" { return fmt.Errorf(\"bad download url\") }","typeGuard":null,"tryCatchPattern":"var path string\nvar err error\nfor i := 0; i < 3; i++ {\n    path, err = DownloadFileToTemp(url, dir)\n    if err == nil { break }\n    if strings.Contains(err.Error(), \"status code 5\") { time.Sleep(backoff(i)); continue }\n    return err // 4xx: do not retry\n}","preventionTips":["Regenerate signed URLs immediately before download","Check response status semantics: retry 5xx, fix 4xx","Verify URL config with a HEAD request at startup","Add User-Agent and proxy settings if behind corporate proxies"],"tags":["http","download","networking"],"backgroundTag":"http-non-200-response","analyzedSha":"6e04ca5ff03222a699c24205cd4bf8fee9af7ffe","analyzedAt":"2026-09-07T15:44:59.941Z","contentChangedAt":"2026-09-07T15:44:59.941Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}