karatelabs/karate · error · RuntimeException

readAsBytes() needs at least one argument

Error message

readAsBytes() needs at least one argument

What it means

Guard clause inside the karate.readAsBytes() JS binding: the lambda requires a path argument, but it was invoked with zero arguments, so the read cannot proceed. Fires when a script calls readAsBytes() with no path; fix by passing the resource path relative to the current feature/resource.

Solutions

  1. Provide the path: karate.readAsBytes('classpath:files/report.pdf').
  2. Ensure the path variable is defined and non-empty.
  3. Confirm the resource exists under the resolved root (classpath:, file:, or relative).

Example fix

// before
var bytes = karate.readAsBytes();
// after
var bytes = karate.readAsBytes('classpath:files/report.pdf');
Defensive patterns

Strategy: validation

Validate before calling

if (!binPath) throw new Error('readAsBytes requires a path');
var bytes = karate.readAsBytes('classpath:' + binPath);

Type guard

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

Try / catch

try { return karate.readAsBytes(p); }
catch (e) { if ((e.message || '').indexOf('readAsBytes() needs at least one argument') >= 0) karate.fail('missing path for readAsBytes'); throw e; }

Prevention

When it happens

Trigger: Calling the readAsBytes JS callable with an empty args array, e.g. karate.readAsBytes().

Common situations: Reading binary files (images, PDFs) for request bodies and forgetting the path; dynamic path construction that ends up passing nothing.

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/248cde5cce0320dc. Report an issue: GitHub.

Appendix: source

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

     * from the same merged map (CLI {@code -D}, Maven/Gradle, and {@code Runner.Builder.systemProperties}).
     */
    private JavaInvokable sysprop() {
        return args -> {
            if (args.length == 0) {
                throw new RuntimeException("sysprop() needs the property name");
            }
            Object fallback = args.length > 1 ? args[1] : null;
            Object first = args[0];
            if (first == null) return fallback;
            String value = getProperty(first.toString());
            return (value == null || value.isEmpty()) ? fallback : value;
        };
    }

    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] + "";

View on GitHub (pinned to a22eb90246)