karatelabs/karate · error · RuntimeException

set() needs at least two arguments: name and value

Error message

set() needs at least two arguments: name and value

What it means

The standard form karate.set(name, value) requires at least two arguments: the variable name and the value to assign. Karate throws this when fewer than two arguments are given and the single argument was not a Map for bulk assignment.

Solutions

  1. Provide both name and value: karate.set('x', 123).
  2. For multiple variables use a single Map argument: karate.set({ x: 1, y: 2 }).
  3. For path-based sets use karate.set('name', 'path', value) with three arguments.
  4. Check that the value variable is defined before the call.

Example fix

// before
karate.set('counter');
// after
karate.set('counter', 0);
Defensive patterns

Strategy: validation

Validate before calling

if (name === undefined || value === undefined) throw new Error('set() needs name and value');
karate.set(name, value);

Type guard

function canSet(name, value) { return typeof name === 'string' && name.length > 0 && arguments.length >= 2; }

Try / catch

try { karate.set(name, value); }
catch (e) { if ((e.message || '').indexOf('set() needs at least two arguments') >= 0) karate.fail('incomplete karate.set call'); throw e; }

Prevention

When it happens

Trigger: karate.set('name') with only a name and no value; karate.set() with nothing (and not a Map).

Common situations: Partial refactor removing the value argument; dynamic value variable undefined leading to a collapsed call; confusing set() with the one-arg bulk-map form.

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/61b211320da2c7a2. Report an issue: GitHub.

Appendix: source

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

    }

    @SuppressWarnings("unchecked")
    private JavaInvokable set() {
        return args -> {
            // v1 bulk form: karate.set(map) sets each top-level key as a variable.
            // Common pattern is `karate.set(read('classpath:settings.json'))`
            // to load a settings file into scope.
            if (args.length == 1) {
                if (args[0] instanceof Map<?, ?> bulk) {
                    for (Map.Entry<?, ?> e : bulk.entrySet()) {
                        engine.put(e.getKey() + "", e.getValue());
                    }
                    return null;
                }
                throw new RuntimeException("set() with a single argument expects a Map / JSON object");
            }
            if (args.length < 2) {
                throw new RuntimeException("set() needs at least two arguments: name and value");
            }
            String name = args[0] + "";
            if (args.length == 2) {
                // Simple set: karate.set('name', value)
                engine.put(name, args[1]);
            } else {
                // Path set: karate.set('name', 'path', value)
                String path = args[1] + "";
                Object value = args[2];
                Object target = engine.get(name);

                // Check if this is XPath (path starts with /) or target is XML
                if (path.startsWith("/") || target instanceof Node) {
                    // XPath set on XML
                    Document doc;
                    if (target instanceof Document) {
                        doc = (Document) target;
                    } else if (target instanceof Node) {

View on GitHub (pinned to a22eb90246)