karatelabs/karate · error · RuntimeException

get() needs at least one argument

Error message

get() needs at least one argument

What it means

Guard clause inside the karate.get() JS binding: it requires at least one argument (the key/name to look up), but was called with none. Fires when a script calls karate.get() without arguments; pass the variable or config key to retrieve.

Solutions

  1. Pass the expression: karate.get('myVar') or karate.get('$.obj.nested').
  2. Remember $-prefixed strings are always JSON-path expressions, not variable names.
  3. Ensure the expression variable is defined before the call.

Example fix

// before
var val = karate.get();
// after
var val = karate.get('$.user.address.city');
Defensive patterns

Strategy: validation

Validate before calling

if (!expr) throw new Error('karate.get requires an expression');
var v = karate.get(expr);

Type guard

function validGetArg(v) { return typeof v === 'string' && v.length > 0; }

Try / catch

var v;
try { v = karate.get(expr); }
catch (e) { if ((e.message || '').indexOf('get() needs at least one argument') >= 0) karate.fail('karate.get called without an expression'); throw e; }

Prevention

When it happens

Trigger: karate.get() with no arguments; building the expression dynamically (e.g. karate.get(exprVar)) where exprVar is undefined so the call has no args.

Common situations: Extracting nested values with karate.get('$.path.to.value') and accidentally deleting the string; refactoring variable names so the argument disappears.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


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

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJs.java:510

    private JavaInvokable readAsBytes() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("readAsBytes() needs at least one argument");
            }
            String path = args[0] + "";
            Resource resource = getCurrentResource().resolve(path);
            try (java.io.InputStream is = resource.getStream()) {
                return is.readAllBytes();
            } catch (java.io.IOException e) {
                throw new RuntimeException("Failed to read bytes from: " + path, e);
            }
        };
    }

    private JavaInvokable get() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("get() needs at least one argument");
            }
            String expr = args[0] + "";

            Object result;
            // a $-prefix always means a path expression, never a variable name - isSimpleIdentifier
            // accepts '$' as a leading char, so without this `karate.get('$x')` would hunt for a
            // variable literally called "$x" instead of resolving the bare `$varname` form
            if (!expr.startsWith("$") && isSimpleIdentifier(expr)) {
                result = engine.get(expr);
            } else {
                // Anything else - a $-prefixed JsonPath, an XPath, `get[N] foo[*].a`, or a
                // plain dot / bracket chain like 'foo.bar.baz' - goes to the same evaluator
                // the Gherkin RHS uses, which is what v1 did (karate.get delegated straight
                // to evalKarateExpression). The hand-rolled JsonPath split that used to live
                // here understood none of the other forms, and handed jayway the raw target
                // so an XML or JSON-string variable silently answered nothing.
                ScenarioRuntime rt = ScenarioRuntime.currentOrNull();
                if (rt == null) {

View on GitHub (pinned to a22eb90246)