hashicorp/packer · error

HTTP request error. Response code

Error message

HTTP request error. Response code: %d

What it means

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.

Solutions

  1. Check the reported status code and fix the URL/path accordingly
  2. Add required authentication via `request_headers` (e.g. Authorization)
  3. Test the URL with curl to confirm the expected 2xx response
  4. If the server redirects, point `url` at the final destination directly

Example fix

// before
url = "https://api.example.com/v1/release" // 404
// after
url = "https://api.example.com/v2/release"
Defensive patterns

Strategy: try-catch

Validate before calling

// curl -I the URL in CI preflight and assert 200
curl -fsS -o /dev/null -w '%{http_code}' "$URL" | grep -q '^2'

Try / catch

// surface status code and body for diagnosis
if !strings.HasPrefix(resp.Status, "2") {
  body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
  return fmt.Errorf("http %d from %s: %s", resp.StatusCode, url, body)
}

Prevention

When it happens

Trigger: The configured URL returns 404 (wrong path), 401/403 (auth required), 500 (server error), 301/302 not followed, etc. during Execute.

Common situations: 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.

Related errors


AI-assisted analysis of hashicorp/packer@eb36e3c3e4 (2026-09-05). Data as JSON: /api/errors/29aadbcb6136e6e3. Report an issue: GitHub.

Appendix: source

Thrown at datasource/http/data.go:159

		fmt.Println("Error creating http request")
		return cty.NullVal(cty.EmptyObject), err
	}

	for name, value := range headers {
		req.Header.Set(name, value)
	}

	resp, err := client.Do(req)
	// TODO: How to make test case for this
	if err != nil {
		fmt.Println("Error making performing http request")
		return cty.NullVal(cty.EmptyObject), err
	}

	defer resp.Body.Close()

	if resp.StatusCode < 200 || resp.StatusCode >= 300 {
		return cty.NullVal(cty.EmptyObject), fmt.Errorf("HTTP request error. Response code: %d", resp.StatusCode)
	}

	contentType := resp.Header.Get("Content-Type")
	if contentType == "" || isContentTypeText(contentType) == false {
		fmt.Printf("Content-Type is not recognized as a text type, got %q\n",
			contentType)
		fmt.Println("If the content is binary data, Packer may not properly handle the contents of the response.")
	}

	bytes, err := io.ReadAll(resp.Body)
	// TODO: How to make test case for this?
	if err != nil {
		fmt.Println("Error processing response body of call")
		return cty.NullVal(cty.EmptyObject), err
	}

	responseHeaders := make(map[string]string)
	for k, v := range resp.Header {

View on GitHub (pinned to eb36e3c3e4)