karatelabs/karate · error · RuntimeException
toBytes() needs one argument: a list of numbers
Error message
toBytes() needs one argument: a list of numbers
What it means
karate.toBytes() converts its argument into a byte[] — numbers become bytes, an existing byte[] is returned as-is. The library throws this error when called with no argument at all, since there is nothing to convert. It is the arity guard before any type inspection happens.
Solutions
- Pass the list of numbers: karate.toBytes([104, 101, 108, 108, 111]).
- Check the variable holding the numbers list is still in scope and passed through.
- If you already have a byte[] in Java, you can pass it directly — it is returned unchanged.
Example fix
// before var bytes = karate.toBytes() // after var bytes = karate.toBytes([72, 73])
Defensive patterns
Strategy: validation
Validate before calling
if (nums == null || !Array.isArray(nums)) { throw new Error('toBytes() requires a list of numbers, got: ' + nums) } Type guard
function isByteList(v) { return v instanceof Array && v.every(n => typeof n === 'number') } Try / catch
try { var bytes = karate.toBytes(nums) } catch (e) { if (('' + e).indexOf('needs one argument') >= 0) { throw new Error('toBytes(): pass the numbers list explicitly') } throw e } Prevention
- Always pass the numbers list explicitly; never invoke karate.toBytes bare
- Keep byte payload construction in a named helper so argument loss is caught once
- Verify the source variable is in scope before the call
When it happens
Trigger: karate.toBytes() with zero arguments, e.g. a dropped argument after refactoring or calling the function reference without invoking arguments.
Common situations: Building binary request bodies (files, certificates) in tests; generating byte payloads dynamically where the source variable was removed or renamed.
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
- toBytes() argument must be a list of numbers, got
- toBytes() list must contain only numbers, got
- read() needs at least one argument
- sysenv() needs the environment-variable name
- sysprop() needs the property name
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/27c6f6bc7e8d6a19.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:559
if (args.length == 0 || args[0] == null) {
return "";
}
if (!(args[0] instanceof List)) {
throw new RuntimeException("toCsv() argument must be a list of maps, got: " + args[0].getClass().getName());
}
List<Map<String, Object>> list = (List<Map<String, Object>>) args[0];
if (list.isEmpty()) {
return "";
}
return DataUtils.toCsv(list);
};
}
@SuppressWarnings("unchecked")
static JavaInvokable toBytes() {
return args -> {
if (args.length < 1) {
throw new RuntimeException("toBytes() needs one argument: a list of numbers");
}
Object arg = args[0];
if (arg instanceof byte[]) {
return arg; // already bytes
}
if (!(arg instanceof List)) {
throw new RuntimeException("toBytes() argument must be a list of numbers, got: " + arg.getClass().getName());
}
List<Object> list = (List<Object>) arg;
byte[] bytes = new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
Object item = list.get(i);
if (item instanceof Number num) {
bytes[i] = num.byteValue();
} else {
throw new RuntimeException("toBytes() list must contain only numbers, got: " + item.getClass().getName() + " at index " + i);
}
}View on GitHub (pinned to a22eb90246)