crowdsecurity/crowdsec · error

invalid type for body: %T

Error message

invalid type for body: %T

What it means

HTTPPost asserts its third parameter (body) to string before sending the request. This error means the body argument was not a string — the helper writes the body via strings.NewReader, so only strings are accepted.

Source

Thrown at pkg/exprhelpers/http.go:104

	return doHTTPRequest(http.MethodHead, uri, nil, nil)
}

// HTTPPost(url string, contentType string, body string) (*HTTPResponse, error)
func HTTPPost(params ...any) (any, error) {
	uri, ok := params[0].(string)
	if !ok {
		return nil, fmt.Errorf("invalid type for url: %T", params[0])
	}

	contentType, ok := params[1].(string)
	if !ok {
		return nil, fmt.Errorf("invalid type for contentType: %T", params[1])
	}

	body, ok := params[2].(string)
	if !ok {
		return nil, fmt.Errorf("invalid type for body: %T", params[2])
	}

	headers := map[string]string{"Content-Type": contentType}

	return doHTTPRequest(http.MethodPost, uri, headers, strings.NewReader(body))
}

// HTTPRequest(method string, url string, headers map[string]any, body string) (*HTTPResponse, error)
func HTTPRequest(params ...any) (any, error) {
	method, ok := params[0].(string)
	if !ok {
		return nil, fmt.Errorf("invalid type for method: %T", params[0])
	}

	uri, ok := params[1].(string)
	if !ok {
		return nil, fmt.Errorf("invalid type for url: %T", params[1])
	}

View on GitHub (pinned to 909b515798)

Solutions

  1. Serialize structured data to a JSON string before passing it (e.g. via a JSON marshal helper in the expression)
  2. Convert numbers or other scalars to string form
  3. Ensure the payload field is populated and string-typed
  4. Check the %T in the error to identify the actual body type

Example fix

// before
HTTPPost(url, "application/json", evt.data) // evt.data is a map
// after
HTTPPost(url, "application/json", ToJSON(evt.data))
Defensive patterns

Strategy: validation

Validate before calling

// serialize before sending:
// HTTPPost(url, "application/json", ToJSON(evt.data))

Type guard

func isString(v interface{}) bool { _, ok := v.(string); return ok }

Prevention

When it happens

Trigger: HTTPPost(url, contentType, evt.payload) where payload is a map, a number, nil, or raw bytes; passing a structured object instead of a serialized string.

Common situations: Passing a parsed JSON object (map[string]interface{}) instead of a serialized JSON string; nil payload field; numeric payload without conversion.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of crowdsecurity/crowdsec@909b515798 (2026-09-06). Data as JSON: /api/errors/ac8f09c8bce6c6c3. Report an issue: GitHub.