{"record":{"id":"248cde5cce0320dc","repo":"karatelabs/karate","slug":"readasbytes-needs-at-least-one-argument","errorCode":null,"errorMessage":"readAsBytes() needs at least one argument","messagePattern":"readAsBytes\\(\\) needs at least one argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":495,"sourceCode":"     * from the same merged map (CLI {@code -D}, Maven/Gradle, and {@code Runner.Builder.systemProperties}).\n     */\n    private JavaInvokable sysprop() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"sysprop() needs the property name\");\n            }\n            Object fallback = args.length > 1 ? args[1] : null;\n            Object first = args[0];\n            if (first == null) return fallback;\n            String value = getProperty(first.toString());\n            return (value == null || value.isEmpty()) ? fallback : value;\n        };\n    }\n\n    private JavaInvokable readAsBytes() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"readAsBytes() needs at least one argument\");\n            }\n            String path = args[0] + \"\";\n            Resource resource = getCurrentResource().resolve(path);\n            try (java.io.InputStream is = resource.getStream()) {\n                return is.readAllBytes();\n            } catch (java.io.IOException e) {\n                throw new RuntimeException(\"Failed to read bytes from: \" + path, e);\n            }\n        };\n    }\n\n    private JavaInvokable get() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"get() needs at least one argument\");\n            }\n            String expr = args[0] + \"\";\n","sourceCodeStart":477,"sourceCodeEnd":513,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L477-L513","documentation":"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.","triggerScenarios":"Calling the readAsBytes JS callable with an empty args array, e.g. karate.readAsBytes().","commonSituations":"Reading binary files (images, PDFs) for request bodies and forgetting the path; dynamic path construction that ends up passing nothing.","solutions":["Provide the path: karate.readAsBytes('classpath:files/report.pdf').","Ensure the path variable is defined and non-empty.","Confirm the resource exists under the resolved root (classpath:, file:, or relative)."],"exampleFix":"// before\nvar bytes = karate.readAsBytes();\n// after\nvar bytes = karate.readAsBytes('classpath:files/report.pdf');","handlingStrategy":"validation","validationCode":"if (!binPath) throw new Error('readAsBytes requires a path');\nvar bytes = karate.readAsBytes('classpath:' + binPath);","typeGuard":"function hasPathArg(args) { return args && args.length > 0 && typeof args[0] === 'string' && args[0].length > 0; }","tryCatchPattern":"try { return karate.readAsBytes(p); }\ncatch (e) { if ((e.message || '').indexOf('readAsBytes() needs at least one argument') >= 0) karate.fail('missing path for readAsBytes'); throw e; }","preventionTips":["Always supply the resource path explicitly.","Confirm the binary resource is on the classpath or at the file path.","Keep binary assets under a stable test-resources directory."],"tags":["javascript","binary-data","argument-validation"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}