karatelabs/karate · error · RuntimeException

redirect() requires a URL argument

Error message

redirect() requires a URL argument

What it means

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.

Solutions

  1. Pass the target URL string: context.redirect('/path').
  2. Validate the URL variable is defined before calling (or supply a default).
  3. If the URL comes from a request parameter, fail fast with a readable message when absent.
  4. Check JS interpolation isn't producing undefined (use string concatenation or explicit checks).

Example fix

// before (JS in markup)
context.redirect(targetUrl); // targetUrl undefined -> 0 args
// after
if (!targetUrl) targetUrl = '/';
context.redirect(targetUrl);
Defensive patterns

Strategy: validation

Validate before calling

// JS in markup
if (typeof url !== 'string' || !url) throw new Error('redirect target required');

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/67d6e0dc2b4e8a4a. Report an issue: GitHub.

Appendix: source

Thrown at karate-core/src/main/java/io/karatelabs/http/ServerMarkupContext.java:349

        }
        return defaultValue;
    }

    // SimpleObject/jsGet implementation

    @Override
    public Object jsGet(String key) {
        // First check MarkupContext base implementation
        Object baseResult = MarkupContext.super.jsGet(key);
        if (baseResult != null) {
            return baseResult;
        }

        // Server-specific methods and properties
        return switch (key) {
            // Methods
            case "redirect" -> (JavaInvokable) args -> {
                if (args.length == 0) throw new RuntimeException("redirect() requires a URL argument");
                redirect(args[0].toString());
                return null;
            };
            case "log" -> (JavaInvokable) args -> {
                log(args);
                return null;
            };
            case "uuid" -> (JavaInvokable) args -> uuid();
            case "init" -> (JavaInvokable) args -> {
                init();
                return session;  // Return session so scripts can do: session = context.init()
            };
            case "close" -> (JavaInvokable) args -> {
                close();
                return null;
            };
            case "switch" -> (JavaInvokable) args -> {
                if (args.length == 0) throw new RuntimeException("switch() requires a template argument");

View on GitHub (pinned to a22eb90246)