karatelabs/karate · error · RuntimeException

missing argument for header()

Error message

missing argument for header()

What it means

response.header() is a JS invokable: the 1-arg form gets a header value, the 2-arg form sets it. Calling it with no arguments is ambiguous and unsupported, so the library throws before dereferencing args[0].

Solutions

  1. Pass the header name: `response.header('Content-Type')` to read.
  2. Pass name and value to set: `response.header('X-Custom', 'v')`; pass null as value to remove.
  3. To list all headers, access the headers collection/map on the response object instead of header().
  4. Guard computed names: `if (name != null) response.header(name, value)`.­

Example fix

// before
response.header()
// after
var ct = response.header('Content-Type')
Defensive patterns

Strategy: validation

Validate before calling

if (name != null) response.header(name, value);

Type guard

function headerNeedsValue(args) { return args.length >= 1; }

Try / catch

try { return response.header(name); } catch (e) { if (('' + e).includes('missing argument')) return null; throw e; }

Prevention

When it happens

Trigger: `response.header()` with zero arguments from JS member access, e.g. `response.header()` intending to read all headers, or a computed name variable that is undefined.

Common situations: Trying to enumerate headers (use the member/map access instead); a typo where the header name argument was dropped during refactoring.

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/1444fcfae28c5e9b. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/http/HttpResponse.java:389

    }

    /**
     * Returns true if this response represents a skipped request.
     * A request is skipped when a listener returns false from HTTP_ENTER event.
     */
    public boolean isSkipped() {
        return status == 0;
    }

    /**
     * 1-arg form: {@code response.header('X')} — returns the last value (String) or null.
     * 2-arg form: {@code response.header('X', 'v')} — sets the header. {@code null}
     * value removes the header. {@code List} value sets all values.
     */
    private JavaInvokable header() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("missing argument for header()");
            }
            String name = args[0] + "";
            if (args.length == 1) {
                return getHeader(name);
            }
            putHeaderValue(name, args[1]);
            return null;
        };
    }

    private JavaInvokable headerValues() {
        return args -> {
            if (args.length > 0) {
                return getHeaderValues(args[0] + "");
            } else {
                throw new RuntimeException("missing argument for headerValues()");
            }
        };

View on GitHub (pinned to a22eb90246)