karatelabs/karate · error · RuntimeException
readAsStream() needs at least one argument
Error message
readAsStream() needs at least one argument
What it means
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.
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
Example fix
// before
var stream = karate.readAsStream(); // fails: no argument
// after
var stream = karate.readAsStream('classpath:data/large.csv'); Defensive patterns
Strategy: type-guard
Validate before calling
// JS in scenario
if (path == null || path === undefined || path === '') {
throw new Error('readAsStream requires a non-empty path argument');
}
var stream = karate.readAsStream(path); Type guard
function hasPathArg(args) {
return Array.isArray(args) && args.length > 0 && args[0] != null && ('' + args[0]) !== '';
} Try / catch
try {
var stream = karate.readAsStream(path);
} catch (e) {
if (('' + e).indexOf('needs at least one argument') !== -1) {
karate.log('readAsStream called without a path');
}
throw e;
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
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
- doc() requires 'read' key with template path
- doc() needs at least one argument
- read() needs at least one argument
- karate.match() needs at least one argument
- karate.call() requires at least one argument (feature path)
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/ec30ee970eb25175.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJs.java:1080
* lookup as well as {@code .status}, {@code .body}, {@code .headers} and friends.
* Returns null before any request has been made.
*/
private Object getResponse() {
if (mockHandler != null) {
// In mock context, 'response' is a variable in the engine being constructed
return engine.get("response");
}
return prevResponse;
}
/**
* karate.readAsStream(path) - Read file as InputStream.
* Useful for streaming large files without loading into memory.
*/
private JavaInvokable readAsStream() {
return args -> {
if (args.length == 0) {
throw new RuntimeException("readAsStream() needs at least one argument");
}
String path = args[0] + "";
Resource resource = getCurrentResource().resolve(path);
try {
return resource.getStream();
} catch (Exception e) {
throw new RuntimeException("Failed to open stream for: " + path, e);
}
};
}
/**
* karate.render(template) - Render HTML template (similar to doc).
* Returns the rendered HTML string without sending to doc consumer.
*/
@SuppressWarnings("unchecked")
private JavaInvokable render() {
return args -> {View on GitHub (pinned to a22eb90246)