{"record":{"id":"29aadbcb6136e6e3","repo":"hashicorp/packer","slug":"http-request-error-response-code-d","errorCode":null,"errorMessage":"HTTP request error. Response code: %d","messagePattern":"HTTP request error\\. Response code: (.+?)","errorType":"http","errorClass":null,"httpStatus":null,"severity":"error","filePath":"datasource/http/data.go","lineNumber":159,"sourceCode":"\t\tfmt.Println(\"Error creating http request\")\n\t\treturn cty.NullVal(cty.EmptyObject), err\n\t}\n\n\tfor name, value := range headers {\n\t\treq.Header.Set(name, value)\n\t}\n\n\tresp, err := client.Do(req)\n\t// TODO: How to make test case for this\n\tif err != nil {\n\t\tfmt.Println(\"Error making performing http request\")\n\t\treturn cty.NullVal(cty.EmptyObject), err\n\t}\n\n\tdefer resp.Body.Close()\n\n\tif resp.StatusCode < 200 || resp.StatusCode >= 300 {\n\t\treturn cty.NullVal(cty.EmptyObject), fmt.Errorf(\"HTTP request error. Response code: %d\", resp.StatusCode)\n\t}\n\n\tcontentType := resp.Header.Get(\"Content-Type\")\n\tif contentType == \"\" || isContentTypeText(contentType) == false {\n\t\tfmt.Printf(\"Content-Type is not recognized as a text type, got %q\\n\",\n\t\t\tcontentType)\n\t\tfmt.Println(\"If the content is binary data, Packer may not properly handle the contents of the response.\")\n\t}\n\n\tbytes, err := io.ReadAll(resp.Body)\n\t// TODO: How to make test case for this?\n\tif err != nil {\n\t\tfmt.Println(\"Error processing response body of call\")\n\t\treturn cty.NullVal(cty.EmptyObject), err\n\t}\n\n\tresponseHeaders := make(map[string]string)\n\tfor k, v := range resp.Header {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/hashicorp/packer/blob/eb36e3c3e48a036f3e8cc94087636ee72e1303c9/datasource/http/data.go#L141-L177","documentation":"After executing the HTTP request, the datasource treats any status outside 2xx as a failure and returns this error with the numeric status code. The response body is discarded, so you only get the code.","triggerScenarios":"The configured URL returns 404 (wrong path), 401/403 (auth required), 500 (server error), 301/302 not followed, etc. during Execute.","commonSituations":"Endpoint moved and returns 404; API requires a token not passed in headers; server-side outage returning 5xx; self-signed cert endpoints proxied behind an error page.","solutions":["Check the reported status code and fix the URL/path accordingly","Add required authentication via `request_headers` (e.g. Authorization)","Test the URL with curl to confirm the expected 2xx response","If the server redirects, point `url` at the final destination directly"],"exampleFix":"// before\nurl = \"https://api.example.com/v1/release\" // 404\n// after\nurl = \"https://api.example.com/v2/release\"","handlingStrategy":"try-catch","validationCode":"// curl -I the URL in CI preflight and assert 200\ncurl -fsS -o /dev/null -w '%{http_code}' \"$URL\" | grep -q '^2'","typeGuard":null,"tryCatchPattern":"// surface status code and body for diagnosis\nif !strings.HasPrefix(resp.Status, \"2\") {\n  body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))\n  return fmt.Errorf(\"http %d from %s: %s\", resp.StatusCode, url, body)\n}","preventionTips":["Add auth headers via request_headers when the endpoint requires them","Monitor target endpoints for moves/404s","Test URLs with curl before embedding them in templates"],"tags":["http","datasource","http-status"],"backgroundTag":"http-non-2xx-response","analyzedSha":"eb36e3c3e48a036f3e8cc94087636ee72e1303c9","analyzedAt":"2026-09-05T13:20:43.127Z","contentChangedAt":"2026-09-05T13:20:43.127Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}