karatelabs/karate · error · RuntimeException

missing argument for file()

Error message

missing argument for file()

What it means

HttpRequest.file() is a script-facing invokable that requires one argument — the name/path of the file part to attach — and forwards it to getFile(). A zero-argument call throws this RuntimeException as an immediate, explicit failure of the DSL contract.

Solutions

  1. Supply the file part identifier: request.file('path/to/file.txt')
  2. Verify the variable holding the file name/path is non-null before the call
  3. Use files() if you intend to pass a JSON of name->path pairs rather than a single file
  4. Grep the script for other zero-arg calls that should carry arguments

Example fix

// before
request.file(); // throws
// after
request.file("upload/data.txt");
Defensive patterns

Strategy: validation

Validate before calling

if (!filePath) throw new Error('file() needs a file path');
request.file(filePath);

Type guard

function hasFileArg(args) { return Array.isArray(args) && args.length > 0 && typeof args[0] === 'string' && args[0].length > 0; }

Prevention

When it happens

Trigger: Calling file() with no arguments, e.g. request.file() instead of request.file('data.txt'); the argument variable is undefined; wrong DSL method selected when the author actually wanted multiPart() or files().

Common situations: Assembling file uploads where the file path comes from config or a placeholder that failed to substitute; migrating scripts between HTTP DSL versions with different signatures; typos in chained builder expressions.

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/012399950219ab34. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/http/HttpRequest.java:685

        };
    }

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

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

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

    /**
     * Forward this request to a target URL and return the response.
     * Mirrors {@code karate.proceed()} so JS-file mocks can implement proxy behavior.
     * If {@code targetUrl} is null, uses the {@code Host} header on this request.

View on GitHub (pinned to a22eb90246)