gofiber/fiber · error

failed to dump request

Error message

failed to dump request: %w

What it means

In app.Test() the request is serialized via httputil.DumpRequest(req, true) before being written to an in-memory testConn. If DumpRequest fails (invalid header bytes, body serialization issues), the wrapped error is returned. This is a TestClient failure, not a runtime server failure.

Solutions

  1. Build requests with http.NewRequest or fiber.NewRequest so headers are validated at construction.
  2. Inspect the wrapped error to find the offending header/body byte.
  3. Avoid setting header values containing CR/LF or control characters.

Example fix

// before
req := &http.Request{Method: "GET", Header: http.Header{"X-Bad": {"a\nb"}}}
resp, _ := app.Test(req)

// after
req := httptest.NewRequest("GET", "/", nil)
req.Header.Set("X-Good", "a-b")
resp, _ := app.Test(req)
Defensive patterns

Strategy: validation

Validate before calling

// Build with http.NewRequest so headers are validated at construction
req, err := http.NewRequest(method, url, body)
if err != nil { return err }

Type guard

func validHeaders(h http.Header) bool {
    for k, vs := range h {
        if !httpguts.ValidHeaderFieldName(k) { return false }
        for _, v := range vs { if !httpguts.ValidHeaderFieldValue(v) { return false } }
    }
    return true
}

Try / catch

resp, err := app.Test(req)
if err != nil {
    return fmt.Errorf("test harness failed to dump request: %w", err)
}

Prevention

When it happens

Trigger: Calling app.Test(req) with an *http.Request that contains headers or a body httputil.DumpRequest cannot serialize (e.g., invalid header characters, control bytes, malformed Content-Length).

Common situations: Hand-building *http.Request with invalid header values; reusing requests across goroutines; bugs in test helpers that set headers from raw user input.

Related errors


AI-assisted analysis of gofiber/fiber@a105acad6c (2026-08-11). Data as JSON: /api/errors/e20980199f3c7a87. Report an issue: GitHub.

Appendix: source

Thrown at app.go:1424

	}

	// Ensure Host header is present in the dump (required by fasthttp)
	if req.Host == "" {
		if req.URL != nil && req.URL.Host != "" {
			req.Host = req.URL.Host
		} else {
			req.Host = "localhost"
		}
	}

	// Clear RequestURI so DumpRequest writes origin-form request line with
	// Host header instead of absolute-form URI without Host header.
	req.RequestURI = ""

	// Dump raw http request
	dump, err := httputil.DumpRequest(req, true)
	if err != nil {
		return nil, fmt.Errorf("failed to dump request: %w", err)
	}

	// Create test connection
	conn := new(testConn)

	// Write raw http request
	if _, err = conn.r.Write(dump); err != nil {
		return nil, fmt.Errorf("failed to write: %w", err)
	}
	// prepare the server for the start
	app.startupProcess()

	// Serve conn to server
	channel := make(chan error, 1)
	go func() {
		var returned bool
		defer func() {
			if !returned {

View on GitHub (pinned to a105acad6c)