public static String extractPathPrefix(final HttpRequest request) {
        final String path = request.getPath();
        try {
            final URIBuilder uriBuilder = new URIBuilder(path);
            uriBuilder.setFragment(null);
            uriBuilder.clearParameters();
            uriBuilder.optimize();
            final List<String> pathSegments = uriBuilder.getPathSegments();

            if (!pathSegments.isEmpty()) {
                pathSegments.remove(pathSegments.size() - 1);
            }
            if (pathSegments.isEmpty()) {
                return "/";
            }
            final StringBuilder buf = new StringBuilder();
            buf.append('/');
            for (final String pathSegment : pathSegments) {
                PercentCodec.encode(buf, pathSegment, StandardCharsets.US_ASCII);
                buf.append('/');
            }
            return buf.toString();
        } catch (final URISyntaxException ex) {
            return path;
        }
    }
Protocol support methods. For internal use only.

@since 5.2
httpclient5/src/main/java/org/apache/hc/client5/http/impl/RequestSupport.java