{"record":{"id":"27c6f6bc7e8d6a19","repo":"karatelabs/karate","slug":"tobytes-needs-one-argument-a-list-of-numbers","errorCode":null,"errorMessage":"toBytes() needs one argument: a list of numbers","messagePattern":"toBytes\\(\\) needs one argument: a list of numbers","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":559,"sourceCode":"            if (args.length == 0 || args[0] == null) {\n                return \"\";\n            }\n            if (!(args[0] instanceof List)) {\n                throw new RuntimeException(\"toCsv() argument must be a list of maps, got: \" + args[0].getClass().getName());\n            }\n            List<Map<String, Object>> list = (List<Map<String, Object>>) args[0];\n            if (list.isEmpty()) {\n                return \"\";\n            }\n            return DataUtils.toCsv(list);\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    static JavaInvokable toBytes() {\n        return args -> {\n            if (args.length < 1) {\n                throw new RuntimeException(\"toBytes() needs one argument: a list of numbers\");\n            }\n            Object arg = args[0];\n            if (arg instanceof byte[]) {\n                return arg; // already bytes\n            }\n            if (!(arg instanceof List)) {\n                throw new RuntimeException(\"toBytes() argument must be a list of numbers, got: \" + arg.getClass().getName());\n            }\n            List<Object> list = (List<Object>) arg;\n            byte[] bytes = new byte[list.size()];\n            for (int i = 0; i < list.size(); i++) {\n                Object item = list.get(i);\n                if (item instanceof Number num) {\n                    bytes[i] = num.byteValue();\n                } else {\n                    throw new RuntimeException(\"toBytes() list must contain only numbers, got: \" + item.getClass().getName() + \" at index \" + i);\n                }\n            }","sourceCodeStart":541,"sourceCodeEnd":577,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L541-L577","documentation":"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.","triggerScenarios":"karate.toBytes() with zero arguments, e.g. a dropped argument after refactoring or calling the function reference without invoking arguments.","commonSituations":"Building binary request bodies (files, certificates) in tests; generating byte payloads dynamically where the source variable was removed or renamed.","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."],"exampleFix":"// before\nvar bytes = karate.toBytes()\n// after\nvar bytes = karate.toBytes([72, 73])","handlingStrategy":"validation","validationCode":"if (nums == null || !Array.isArray(nums)) { throw new Error('toBytes() requires a list of numbers, got: ' + nums) }","typeGuard":"function isByteList(v) { return v instanceof Array && v.every(n => typeof n === 'number') }","tryCatchPattern":"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 }","preventionTips":["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"],"tags":["javascript","argument-validation","bytes"],"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"}