elastic/elasticsearch · error · IllegalArgumentException

pathPrefix is malformed. consecutive slashes are not allowed

Error message

pathPrefix is malformed. consecutive slashes are not allowed: [{}]

What it means

Validation in cleanPathPrefix: the prefix must not contain consecutive slashes ("//"). Such input would silently collapse or produce wrong paths, so it is rejected outright rather than normalized.

Source

Thrown at client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java:220

     * it is not intended for other purposes and it should not be supplied in other scenarios.
     *
     * @throws NullPointerException if {@code pathPrefix} is {@code null}.
     * @throws IllegalArgumentException if {@code pathPrefix} is empty or contains consecutive slashes.
     */
    public RestClientBuilder setPathPrefix(String pathPrefix) {
        this.pathPrefix = cleanPathPrefix(pathPrefix);
        return this;
    }

    public static String cleanPathPrefix(String pathPrefix) {
        Objects.requireNonNull(pathPrefix, "pathPrefix must not be null");

        if (pathPrefix.isEmpty()) {
            throw new IllegalArgumentException("pathPrefix must not be empty");
        }

        if (pathPrefix.contains("//")) {
            throw new IllegalArgumentException("pathPrefix is malformed. consecutive slashes are not allowed: [" + pathPrefix + "]");
        }

        String cleanPathPrefix = pathPrefix;
        if (cleanPathPrefix.startsWith("/") == false) {
            cleanPathPrefix = "/" + cleanPathPrefix;
        }

        // best effort to ensure that it looks like "/base/path" rather than "/base/path/"
        if (cleanPathPrefix.endsWith("/") && cleanPathPrefix.length() > 1) {
            cleanPathPrefix = cleanPathPrefix.substring(0, cleanPathPrefix.length() - 1);
        }
        return cleanPathPrefix;
    }

    /**
     * Sets the {@link NodeSelector} to be used for all requests.
     * @throws NullPointerException if the provided nodeSelector is null
     */

View on GitHub (pinned to db6a809a66)

Solutions

  1. Build the prefix from segments joined by a single slash, or collapse '//' to '/' yourself before passing.
  2. Strip trailing slashes from each config segment before joining.
  3. Run a quick sanity check: prefix.indexOf("//") < 0.

Example fix

// before
builder.setPathPrefix(base + "/" + sub); // base ends with '/', sub starts with '/'
// after
String joined = (base + "/" + sub).replaceAll("/+", "/");
builder.setPathPrefix(joined);
Defensive patterns

Strategy: validation

Validate before calling

String p = prefix.trim().replaceAll("/+", "/");
if (!p.isEmpty() && !p.contains("//")) builder.setPathPrefix(p);

Type guard

prefix == null || (!prefix.isEmpty() && !prefix.contains("//"))

Prevention

When it happens

Trigger: Calling setPathPrefix("/base//path") or any prefix where a user joined segments with extra slashes.

Common situations: Concatenating "base/" + "/sub" from config fields; copy-paste of a URL path with double slash; trailing-slash handling code that prepended a slash twice.

Understand the failure class

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/7eb5d12f5840119c. Report an issue: GitHub.