karatelabs/karate · error · RuntimeException

http method expression evaluated to null:

Error message

http method expression evaluated to null: 

What it means

For a 'method' step whose text is not a literal HTTP verb, Karate evaluates the text as an expression expecting a string like 'get' or 'post'. If the expression evaluates to null, Karate throws this RuntimeException because there is no HTTP method to execute.

Solutions

  1. Define the variable before the method step: 'def httpMethod = "get"'
  2. Print the variable right before the step to confirm it is non-null
  3. Check the spelling of the variable and that the source feature/JSON actually sets it
  4. Add a guard assertion earlier: 'assert httpMethod != null' or use karate's match to validate setup

Example fix

// before: variable undefined -> null -> error
When method httpVerb
// after
def httpVerb = 'post'
When method httpVerb
Defensive patterns

Strategy: validation

Validate before calling

// ensure the method variable resolves before the method step
Object m = karate.get('httpMethod');
if (m == null) throw new IllegalStateException("httpMethod variable must be defined before 'method' step");

Type guard

static boolean hasMethodVar(io.karatelabs.core.ScenarioEngine k) { return k.get("httpMethod") != null; }

Try / catch

try { executor.execute(methodStep); } catch (RuntimeException e) { if (e.getMessage().startsWith("http method expression evaluated to null")) { throw new IllegalStateException("define the method variable: " + e.getMessage()); } throw e; }

Prevention

When it happens

Trigger: Using 'method httpMethod' (or 'method config.method') where the referenced variable is undefined or evaluates to null at runtime — e.g. the variable was never 'def'ined, or a JSON path resolved to nothing.

Common situations: Typos in variable names; variables expected to be set by a called feature or data-driven setup that didn't run; reading a method name from a config/JSON key that is absent; case where the value is null after a failed match.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/4fd5524754a23723. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/StepExecutor.java:2154

            Object result = runtime.eval(text.trim());
            action = result != null ? result.toString() : "";
        }
        http().header("SOAPAction", action);
        http().contentType("text/xml");
        doMethod("POST");
    }

    private static final Set<String> HTTP_METHODS = Set.of(
            "GET", "POST", "PUT", "DELETE", "PATCH", "HEAD", "OPTIONS", "CONNECT", "TRACE");

    private void executeMethod(Step step) {
        String text = step.getText().trim();
        String method = text.toUpperCase();
        if (!HTTP_METHODS.contains(method) && !isLiteralMethod(text)) {
            // e.g. `method httpMethod` where httpMethod is a variable holding 'get'
            Object result = runtime.eval(text, step);
            if (result == null) {
                throw new RuntimeException("http method expression evaluated to null: " + text);
            }
            method = result.toString().trim().toUpperCase();
        }
        doMethod(method);
    }

    /**
     * True when the text after {@code method} should be taken as the verb itself instead of
     * being evaluated. A bare word that is not a variable is a custom verb such as PURGE or
     * PROPFIND - anything else (a variable name, a quoted string, a function call) is an
     * expression that resolves to the verb.
     */
    private boolean isLiteralMethod(String text) {
        for (int i = 0; i < text.length(); i++) {
            if (!Character.isLetter(text.charAt(i))) {
                return false;
            }
        }

View on GitHub (pinned to a22eb90246)