crowdsecurity/crowdsec · error

invalid type for contentType: %T

Error message

invalid type for contentType: %T

What it means

The HTTPPost expr helper was called with params[1] (the contentType argument) not a string. This is a type-check guard for the variadic ...any signature — the expression passed e.g. a number where the body content-type string is expected; %T reveals the actual type.

Source

Thrown at pkg/exprhelpers/http.go:99

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

	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])
	}

View on GitHub (pinned to 909b515798)

Solutions

  1. Pass a literal string content type such as "application/json"
  2. If the content type is dynamic, ensure the field is a string before calling
  3. Check argument order: HTTPPost(url, contentType, body)
  4. Inspect the %T in the message to confirm which wrong type was supplied

Example fix

// before
HTTPPost("http://example.com", application_json, "{}")
// after
HTTPPost("http://example.com", "application/json", "{}")
Defensive patterns

Strategy: type-guard

Validate before calling

// always pass a string literal:
// HTTPPost(url, "application/json", body)

Type guard

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

Prevention

When it happens

Trigger: HTTPPost(url, 42, body), HTTPPost(url, nil, body), or passing a variable that holds a non-string value in the contentType position.

Common situations: Content type field read from config/event as non-string; argument order mistake (e.g. body passed as second arg, shifting contentType); nil placeholder used for an optional header.

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/0afa00461ff1e90a. Report an issue: GitHub.