{"record":{"id":"67d6e0dc2b4e8a4a","repo":"karatelabs/karate","slug":"redirect-requires-a-url-argument","errorCode":null,"errorMessage":"redirect() requires a URL argument","messagePattern":"redirect\\(\\) requires a URL argument","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java","lineNumber":349,"sourceCode":"        }\n        return defaultValue;\n    }\n\n    // SimpleObject/jsGet implementation\n\n    @Override\n    public Object jsGet(String key) {\n        // First check MarkupContext base implementation\n        Object baseResult = MarkupContext.super.jsGet(key);\n        if (baseResult != null) {\n            return baseResult;\n        }\n\n        // Server-specific methods and properties\n        return switch (key) {\n            // Methods\n            case \"redirect\" -> (JavaInvokable) args -> {\n                if (args.length == 0) throw new RuntimeException(\"redirect() requires a URL argument\");\n                redirect(args[0].toString());\n                return null;\n            };\n            case \"log\" -> (JavaInvokable) args -> {\n                log(args);\n                return null;\n            };\n            case \"uuid\" -> (JavaInvokable) args -> uuid();\n            case \"init\" -> (JavaInvokable) args -> {\n                init();\n                return session;  // Return session so scripts can do: session = context.init()\n            };\n            case \"close\" -> (JavaInvokable) args -> {\n                close();\n                return null;\n            };\n            case \"switch\" -> (JavaInvokable) args -> {\n                if (args.length == 0) throw new RuntimeException(\"switch() requires a template argument\");","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java#L331-L367","documentation":"The server context object exposed to markup JS provides a redirect() method; calling it with zero arguments throws this RuntimeException. The library requires exactly a URL argument to build the redirect response.","triggerScenarios":"Calling context.redirect() with no arguments in server markup JavaScript (e.g. inside a handler template), typically because the URL variable was undefined/empty and JS dropped the argument, or a plain typo.","commonSituations":"Login/CSRF flows in Karate mock servers that redirect after authentication; templating errors where the redirect target is interpolated from a missing request parameter; tests testCsrfTokenDisabled and testContextGlobalIsolatedAcrossRequests exercise this surface.","solutions":["Pass the target URL string: context.redirect('/path').","Validate the URL variable is defined before calling (or supply a default).","If the URL comes from a request parameter, fail fast with a readable message when absent.","Check JS interpolation isn't producing undefined (use string concatenation or explicit checks)."],"exampleFix":"// before (JS in markup)\ncontext.redirect(targetUrl); // targetUrl undefined -> 0 args\n// after\nif (!targetUrl) targetUrl = '/';\ncontext.redirect(targetUrl);","handlingStrategy":"validation","validationCode":"// JS in markup\nif (typeof url !== 'string' || !url) throw new Error('redirect target required');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass an explicit URL string to context.redirect()","Default missing targets to a safe path like '/'","Avoid interpolating possibly-undefined variables into call arguments","Test redirect flows in the mock server"],"tags":["server","javascript","missing-argument","redirect"],"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"}