{"record":{"id":"ec30ee970eb25175","repo":"karatelabs/karate","slug":"readasstream-needs-at-least-one-argument","errorCode":null,"errorMessage":"readAsStream() needs at least one argument","messagePattern":"readAsStream\\(\\) 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":1080,"sourceCode":"     * lookup as well as {@code .status}, {@code .body}, {@code .headers} and friends.\n     * Returns null before any request has been made.\n     */\n    private Object getResponse() {\n        if (mockHandler != null) {\n            // In mock context, 'response' is a variable in the engine being constructed\n            return engine.get(\"response\");\n        }\n        return prevResponse;\n    }\n\n    /**\n     * karate.readAsStream(path) - Read file as InputStream.\n     * Useful for streaming large files without loading into memory.\n     */\n    private JavaInvokable readAsStream() {\n        return args -> {\n            if (args.length == 0) {\n                throw new RuntimeException(\"readAsStream() needs at least one argument\");\n            }\n            String path = args[0] + \"\";\n            Resource resource = getCurrentResource().resolve(path);\n            try {\n                return resource.getStream();\n            } catch (Exception e) {\n                throw new RuntimeException(\"Failed to open stream for: \" + path, e);\n            }\n        };\n    }\n\n    /**\n     * karate.render(template) - Render HTML template (similar to doc).\n     * Returns the rendered HTML string without sending to doc consumer.\n     */\n    @SuppressWarnings(\"unchecked\")\n    private JavaInvokable render() {\n        return args -> {","sourceCodeStart":1062,"sourceCodeEnd":1098,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L1062-L1098","documentation":"karate.readAsStream(path) reads a file relative to the current resource context and returns it as an InputStream for streaming large files. The method requires exactly one path argument; Karate throws this error immediately when it is called with zero arguments, before attempting any file resolution.","triggerScenarios":"Invoking karate.readAsStream() with no arguments — e.g. a typo'd variable means the intended path argument is missing (karate.readAsStream()), or the call was forwarded from a wrapper function that dropped its arguments.","commonSituations":"Refactoring a helper like function read(p) { return karate.readAsStream(); } and forgetting to thread the parameter through; calling readAsStream conditionally with an undefined JS variable which some setups coerce to a zero-arg call; confusing readAsStream() with karate.read() used without parens elsewhere.","solutions":["Pass the file path as the first argument: karate.readAsStream('data/large.csv')","If wrapping in a function, forward the parameter: function read(p) { return karate.readAsStream(p); }","Use karate.read(path) instead if you want the whole file content in memory rather than a stream","Check the calling code for an undefined variable silently swallowing the argument"],"exampleFix":"// before\nvar stream = karate.readAsStream(); // fails: no argument\n\n// after\nvar stream = karate.readAsStream('classpath:data/large.csv');","handlingStrategy":"type-guard","validationCode":"// JS in scenario\nif (path == null || path === undefined || path === '') {\n  throw new Error('readAsStream requires a non-empty path argument');\n}\nvar stream = karate.readAsStream(path);","typeGuard":"function hasPathArg(args) {\n  return Array.isArray(args) && args.length > 0 && args[0] != null && ('' + args[0]) !== '';\n}","tryCatchPattern":"try {\n  var stream = karate.readAsStream(path);\n} catch (e) {\n  if (('' + e).indexOf('needs at least one argument') !== -1) {\n    karate.log('readAsStream called without a path');\n  }\n  throw e;\n}","preventionTips":["Pass the path literal directly where possible instead of routing through helper functions","When wrapping, forward all parameters to the karate call","Prefer karate.read() for small files and reserve readAsStream for large ones"],"tags":["javascript","file-io","missing-argument","karate"],"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"}