karatelabs/karate · error · RuntimeException

header() needs two arguments

Error message

header() needs two arguments

What it means

HttpRequestBuilder exposes header() to scripts as a two-argument invokable (name, value) that delegates to header(String,String). A call with fewer than two arguments throws this RuntimeException because a header without a name+value pair is meaningless.

Solutions

  1. Pass both name and value: builder.header('Authorization', 'Bearer ' + token)
  2. Check argument-producing expressions (splits, destructuring) aren't yielding a single element
  3. If the value is optional, conditionally call header() only when the value exists
  4. For multiple headers pass a map via the appropriate overload instead of repeated partial calls

Example fix

// before
builder.header("Authorization"); // throws
// after
builder.header("Authorization", "Bearer " + token);
Defensive patterns

Strategy: validation

Validate before calling

if (headerValue == null) throw new Error('header ' + headerName + ' has no value; skip or set it');
builder.header(headerName, headerValue);

Type guard

function headerArgsOk(name, value) { return name != null && value != null; }

Prevention

When it happens

Trigger: Calling header() or header('Authorization') with only one argument from a script binding; undefined/empty variables collapsing the argument count; mixing up header(name, value) with the header-getter overloads on other classes.

Common situations: Chained request building in JS where a values array was empty; dynamic header loops that skip empty values but still call header() once; porting code between Karate's DSL and raw Java header APIs with different signatures.

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/17edcf6d85e062ac. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/http/HttpRequestBuilder.java:720

        return sb.toString();
    }

    private JavaInvokable method() {
        return args -> {
            if (args.length > 1) {
                body = args[1];
            }
            if (args.length > 0) {
                method = args[0] + "";
            }
            return invoke();
        };
    }

    private JavaInvokable header() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("header() needs two arguments");
            }
            header(args[0] + "", args[1] + "");
            return this;
        };
    }

    private JavaInvokable param() {
        return args -> {
            if (args.length < 2) {
                throw new RuntimeException("param() needs two arguments");
            }
            param(args[0] + "", args[1] + "");
            return this;
        };
    }

    /**
     * The JS-facing plural forms of {@link #param} and {@link #header}. The Java

View on GitHub (pinned to a22eb90246)