{"record":{"id":"b4b150d5f9f15933","repo":"karatelabs/karate","slug":"unsupported-response-body-type","errorCode":null,"errorMessage":"unsupported response body type: ","messagePattern":"unsupported response body type: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/HttpResponse.java","lineNumber":529,"sourceCode":"     * static factories ({@link #json(Object)}, {@link #text(String)}, etc.).\n     */\n    public void setBodyDynamic(Object value) {\n        applyJsBody(this, value);\n    }\n\n    private static void applyJsBody(HttpResponse r, Object value) {\n        if (value == null) {\n            r.setBody((byte[]) null, null);\n        } else if (value instanceof byte[] bytes) {\n            r.setBody(bytes, null);\n        } else if (value instanceof String s) {\n            r.setBody(s, ResourceType.TEXT);\n        } else if (value instanceof Node xml) {\n            r.setBody(Xml.toString(xml), ResourceType.XML);\n        } else if (value instanceof Map<?, ?> || value instanceof List<?>) {\n            r.setBody(FileUtils.toBytes(JSONValue.toJSONString(value)), ResourceType.JSON);\n        } else {\n            throw new RuntimeException(\"unsupported response body type: \" + value);\n        }\n    }\n\n    // ========== Static factories ==========\n    // Build a response in one call. Status + body + Content-Type set atomically\n    // — no order trap, no hidden setBody overloads.\n\n    /** {@code 200 OK} with no body. */\n    public static HttpResponse ok() {\n        HttpResponse r = new HttpResponse();\n        r.setStatus(200);\n        return r;\n    }\n\n    /**\n     * {@code 200 OK} with body type inferred (matches the JS-mock dispatch):\n     * {@code String→text/plain}, {@code Map/List→application/json},\n     * {@code Node→application/xml}, {@code byte[]→no Content-Type}.","sourceCodeStart":511,"sourceCodeEnd":547,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/HttpResponse.java#L511-L547","documentation":"When building a mock/HTTP response via response.body(value) (applyJsBody), the library converts strings to TEXT, XML Nodes to XML, and Maps/Lists to JSON. Any other JS value type has no defined serialization, so the library throws listing the offending value.","triggerScenarios":"Passing a number, boolean, JS function, Java object, byte array, or undefined as the response body value from a mock response script.","commonSituations":"Returning a computed numeric/boolean value from a mock handler; forgetting to JSON.stringify or convert a custom object; variable shadowing leaving a function as the body.","solutions":["Wrap scalars in JSON: pass `{ value: 42 }` or a JSON string `Json.stringify(x)`.","Convert Maps/Lists pass-through as-is; for primitives build a JSON string: `response.body(JSON.stringify(42))`.","For binary data use the byte-array-aware setBody API instead of the JS body() hook.","Type-check in the mock script before assigning the body.­"],"exampleFix":"// before\nresponse.body(42) // unsupported\n// after\nresponse.body(JSON.stringify({ value: 42 }))","handlingStrategy":"type-guard","validationCode":"function toResponseBody(v) { if (v == null) return null; if (typeof v === 'string' || v instanceof Map || Array.isArray(v)) return v; return JSON.stringify(v); }","typeGuard":"function isSupportedBodyType(v) { return typeof v === 'string' || typeof v === 'object' && v !== null; }","tryCatchPattern":"try { response.body(v); } catch (e) { if (('' + e).startsWith('unsupported response body type')) response.body(JSON.stringify({ value: v })); else throw e; }","preventionTips":["Only pass strings, XML nodes, Maps, or Lists as response bodies","Stringify primitives and custom objects before assigning","In mock handlers, normalize the computed body once in a helper"],"tags":["http","mock","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-19T12:17:13.211Z"}