VictoriaMetrics/VictoriaMetrics · error

unexpected status code returned from %q: %d; expecting %d; r

Error message

unexpected status code returned from %q: %d; expecting %d; response body: %q

What it means

snapshot.Create expects the snapshot API to answer HTTP 200 with a JSON snapshot descriptor. Any other status code produces this error containing the redacted URL, the actual status, the expected 200, and the response body — which typically holds VictoriaMetrics' own error explanation. The URL is Redacted() so credentials are not leaked.

Source

Thrown at lib/snapshot/snapshot.go:60

	if err != nil {
		return "", fmt.Errorf("cannot create http client for -snapshot.createURL=%q: %w", createSnapshotURL, err)
	}
	req, err := http.NewRequestWithContext(ctx, http.MethodPost, createSnapshotURL, nil)
	if err != nil {
		return "", fmt.Errorf("cannot create request for -snapshot.createURL=%q: %w", createSnapshotURL, err)
	}
	resp, err := hc.Do(req)
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return "", err
	}
	if resp.StatusCode != http.StatusOK {
		return "", fmt.Errorf("unexpected status code returned from %q: %d; expecting %d; response body: %q", u.Redacted(), resp.StatusCode, http.StatusOK, body)
	}

	snap := snapshot{}
	err = json.Unmarshal(body, &snap)
	if err != nil {
		return "", fmt.Errorf("cannot parse JSON response from %q: %w; response body: %q", u.Redacted(), err, body)
	}

	if snap.Status == "ok" {
		logger.Infof("Snapshot %s created", snap.Snapshot)
		return snap.Snapshot, nil
	}
	if snap.Status == "error" {
		return "", fmt.Errorf("snapshot status: %q; msg: %q", snap.Status, snap.Msg)
	}
	return "", fmt.Errorf("snapshot status unknown: %q", snap.Status)
}

View on GitHub (pinned to 5079fb58f1)

Solutions

  1. Read the response body in the error — VictoriaMetrics returns a descriptive message for the failure.
  2. Verify the URL path is /snapshot/create on the correct VictoriaMetrics (or vmselect) port.
  3. Add authentication flags/credentials if the server returns 401/403.
  4. Check VictoriaMetrics server logs at the same timestamp for the underlying storage/snapshot error.
  5. Confirm no reverse proxy/ingress intercepts the request (look for 403/404/502 from the proxy instead of VM).

Example fix

// before
-snapshot.createURL=http://vm:8428/snapshots/create   // 404
// after
-snapshot.createURL=http://vm:8428/snapshot/create    // correct API path
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-check endpoint reachability
resp, err := http.Get(baseURL + "/health")
if err != nil || resp.StatusCode != 200 {
	log.Fatalf("endpoint not healthy")
}

Try / catch

name, err := snapshot.Create(ctx, createURL)
if err != nil && strings.Contains(err.Error(), "unexpected status code") {
	// parse body from error message / check server logs, then correct auth or path
	log.Fatalf("snapshot API rejected request: %v", err)
}

Prevention

When it happens

Trigger: The POST to -snapshot.createURL returns a non-200 status: 404 (wrong path or non-VictoriaMetrics server), 403 (auth required), 400, or 5xx from the storage node.

Common situations: Pointing -snapshot.createURL at the wrong service/port, missing -httpAuth.* credentials when the server has auth enabled, or a reverse proxy returning 403/502, or snapshot creation failing server-side due to storage issues.

Related errors


AI-assisted analysis of VictoriaMetrics/VictoriaMetrics@5079fb58f1 (2026-09-03). Data as JSON: /api/errors/517a987765b5c112. Report an issue: GitHub.