{"record":{"id":"e66bd53c26efe56e","repo":"karatelabs/karate","slug":"karate-request-is-only-available-in-mock-context","errorCode":null,"errorMessage":"karate.request is only available in mock context","messagePattern":"karate\\.request is only available in mock context","errorType":"console","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJs.java","lineNumber":1051,"sourceCode":"            if (body != null) {\n                builder.body(body);\n            }\n\n            // Execute and return the response\n            HttpResponse response = builder.invoke();\n            return response;\n        };\n    }\n\n    // ========== Pending Methods Implementation ==========\n\n    /**\n     * karate.request - Returns the current mock request (only in mock context).\n     * Returns the body content of the current request.\n     */\n    private Object getRequest() {\n        if (mockHandler == null) {\n            logger.warn(\"karate.request is only available in mock context\");\n            return null;\n        }\n        io.karatelabs.http.HttpRequest request = mockHandler.getCurrentRequest();\n        return request != null ? request.getBodyConverted() : null;\n    }\n\n    /**\n     * karate.response - In mock context, returns the response variable being constructed\n     * (a writable parsed body). In non-mock (test) context, returns the previous {@code HttpResponse}\n     * received from an HTTP call, exposing {@code .header(name)} for case-insensitive header\n     * lookup as well as {@code .status}, {@code .body}, {@code .headers} and friends.\n     * Returns null before any request has been made.\n     */\n    private Object getResponse() {\n        if (mockHandler != null) {\n            // In mock context, 'response' is a variable in the engine being constructed\n            return engine.get(\"response\");\n        }","sourceCodeStart":1033,"sourceCodeEnd":1069,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJs.java#L1033-L1069","documentation":"karate.request is only meaningful when Karate is acting as an HTTP mock server: it returns the body of the request currently being handled by the mock. KarateJs's getRequest() checks for a mock handler; if the KarateJs instance is not wired to a MockHandler, it logs this warning and returns null instead of throwing.","triggerScenarios":"Evaluating `karate.request` (a JS property getter backed by getRequest()) inside a scenario, JS block, or embedded KarateJs instance where mockHandler is null — i.e. any non-mock execution: normal API test run, UI run, or custom runner without a mock attached.","commonSituations":"Copying mock-request helper code (meant for karate.mock()/MockHandler scenarios) into a regular feature test; running the same feature both as a mock handler and as a client test; typo in setup where the mock server was never started before evaluating the JS.","solutions":["Only reference karate.request inside mock-handler context (e.g. code evaluated by the mock when a request arrives).","Start/wire the mock (karate.start / MockHandler) before code that reads karate.request runs.","Guard the JS: `if (karate.request) { ... }` or read the actual request object passed into your mock handler callback instead.","If you meant the current HTTP response/request in a client test, use the correct variable (e.g. response / request in feature scope), not karate.request."],"exampleFix":"// before (client-side test)\nvar body = karate.request; // warns, always null outside mock\n\n// after (inside mock handler context)\n// mock handler JS:\nfunction fn(request) { return { body: request.body }; }\n// or in mock scenario: read the built-in request variable, not karate.request","handlingStrategy":"type-guard","validationCode":"// JS guard before use\nvar req = karate.request;\nif (req !== null && typeof req === 'object') { /* in mock context */ }","typeGuard":"function inMockContext(karate) { try { return karate.request != null; } catch (e) { return false; } }","tryCatchPattern":"// returns null (does not throw); null-check the property read\nvar body = karate.request;\nif (body == null) {\n  // not in mock context — use feature-scope request/response instead\n}","preventionTips":["Only read karate.request in mock-handler scenarios/callbacks, never in client-side tests.","Start the mock server (karate.start / MockHandler) before any code that reads karate.request.","Prefer the request object passed directly to your mock handler function over the global property."],"tags":["http","mock","karate","javascript"],"backgroundTag":"invalid-state-transition","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"}