{"record":{"id":"ff9cb26500468dd2","repo":"karatelabs/karate","slug":"tobytes-argument-must-be-a-list-of-numbers-got-classname","errorCode":null,"errorMessage":"toBytes() argument must be a list of numbers, got: {className}","messagePattern":"toBytes\\(\\) argument must be a list of numbers, got: (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":566,"sourceCode":"            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            }\n            return bytes;\n        };\n    }\n\n    static JavaInvokable toJson() {\n        return args -> {\n            if (args.length < 1) {","sourceCodeStart":548,"sourceCodeEnd":584,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L548-L584","documentation":"karate.toBytes() accepts a byte[] or a List; anything else cannot be interpreted as a byte sequence, so the library throws this error with the actual Java class name of the argument. This is the top-level type guard after the arity check and before element-wise conversion.","triggerScenarios":"karate.toBytes('hello') passing a string; karate.toBytes(123) passing a number; passing a Map or other non-List, non-byte[] object.","commonSituations":"Trying to encode a string as bytes with toBytes (use a different mechanism or convert to char codes); passing JSON directly instead of a numeric array; wrong variable ordering in a script.","solutions":["Pass a List of numbers instead: karate.toBytes([72, 105]).","If you have a string, convert it to char codes first, or use string-to-byte helpers elsewhere in the API.","Check what the argument actually is at runtime; the message's class name tells you which type leaked in."],"exampleFix":"// before\nvar bytes = karate.toBytes('hello')\n// after\nvar bytes = karate.toBytes([104, 101, 108, 108, 111])","handlingStrategy":"type-guard","validationCode":"if (arg != null && !Array.isArray(arg) && !(arg instanceof (Java.type('byte[]').class) || true)) { /* in JS, check for List-like */ }\nif (typeof arg === 'string' || typeof arg === 'number') { throw new Error('toBytes() needs a list of numbers, not ' + typeof arg) }","typeGuard":"function isByteList(v) { return v != null && Array.isArray(v) }","tryCatchPattern":"try { var bytes = karate.toBytes(arg) } catch (e) { if (('' + e).indexOf('must be a list of numbers') >= 0) { karate.logger.warn('toBytes got: ' + ('' + e)); bytes = null } throw e }","preventionTips":["Never pass raw strings to toBytes — convert to char codes first","Check the runtime type of your variable (the error message names the Java class)","Use toBytes only for numeric arrays or existing byte arrays"],"tags":["javascript","argument-validation","bytes","type-error"],"backgroundTag":"type-mismatch","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"}