crowdsecurity/crowdsec · error

invalid type for method: %T

Error message

invalid type for method: %T

What it means

HTTPRequest(method, url, headers, body) validates each argument's type in order; this error means the first argument (the HTTP method) was not a string. The method must be a string like 'GET', 'POST'.

Source

Thrown at pkg/exprhelpers/http.go:116

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

	// headers is map[string]any so that expr map literals (map[string]interface{})
	// are accepted; values are stringified.
	rawHeaders, ok := params[2].(map[string]any)
	if !ok {
		return nil, fmt.Errorf("invalid type for headers: %T", params[2])
	}

	headers := make(map[string]string, len(rawHeaders))
	for k, v := range rawHeaders {
		headers[k] = fmt.Sprint(v)
	}

View on GitHub (pinned to 909b515798)

Solutions

  1. Pass a quoted string literal for the method, e.g. "GET"
  2. If the method is dynamic, ensure the field is a string before the call
  3. Verify argument order: HTTPRequest(method, url, headers, body)
  4. Use the %T value in the message to see what was actually passed

Example fix

// before
HTTPRequest(evt.http_verb, url, {}, "")
// after
HTTPRequest("GET", url, {}, "")
Defensive patterns

Strategy: validation

Validate before calling

// pass a string literal: HTTPRequest("GET", ...)

Type guard

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

Prevention

When it happens

Trigger: HTTPRequest(evt.method, url, {}, "") where evt.method is nil or non-string; passing an enum-like constant of non-string type; swapping the first two arguments.

Common situations: Method field missing on the event so it evaluates to nil; accidentally passing the URL as first argument; method stored as a parsed token of non-string type.

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/286031544c872919. Report an issue: GitHub.