karatelabs/karate · error · RuntimeException

Failed to fetch content from URL

Error message

Failed to fetch content from URL: {url}

What it means

Thrown when Resource resolves an http/https URL: it opens the URL stream and buffers the bytes, and any failure in that network fetch (connect error, 4xx/5xx via IOException, TLS failure, timeout) is wrapped as 'Failed to fetch content from URL: {url}'.

Solutions

  1. Open the URL in a browser or 'curl -I <url>' to confirm it is reachable and returns 200
  2. Check DNS/proxy settings and add JVM proxy flags if behind a corporate proxy (-Dhttps.proxyHost, -Dhttps.proxyPort)
  3. Correct the URL (scheme, host, port, path) and retry
  4. Prefer downloading the resource to a local file once, then use a file/classpath resource, for hermetic test runs

Example fix

// before
Resource r = Resource.fromUrl(new URL("http://configs.example.com/karate-base.js"));
// after
HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
c.setConnectTimeout(5000);
if (c.getResponseCode() != 200) {
    throw new IllegalStateException("resource unavailable, HTTP " + c.getResponseCode());
}
Resource r = Resource.fromUrl(new URL(url));
Defensive patterns

Strategy: try-catch

Validate before calling

URL u = new URL(url);
if (!"http".equals(u.getProtocol()) && !"https".equals(u.getProtocol())) {
    throw new IllegalArgumentException("expecting http/https URL: " + url);
}
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setConnectTimeout(5000); c.setReadTimeout(5000);
if (c.getResponseCode() != 200) throw new IllegalStateException("HTTP " + c.getResponseCode());

Try / catch

try {
    Resource r = Resource.fromUrl(new URL(url));
} catch (RuntimeException e) {
    if (e.getCause() instanceof java.io.IOException io) {
        throw new ServiceException("remote resource unreachable: " + url, io);
    }
    throw e;
}

Prevention

When it happens

Trigger: Calling the URL-based resource factory with an http/https URL whose openStream() or byte read throws — server down, wrong host/port, DNS failure, TLS handshake failure, or a 404 that surfaces as a FileNotFoundException from openStream.

Common situations: Referencing remote feature/data files by URL in CI where the network is restricted; typos in hostnames; server returning errors; corporate proxies requiring configuration (-Dhttp.proxyHost etc.).

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/common/Resource.java:854

    /**
     * Creates a Resource from a URL with custom root.
     * Supports file://, jar://, http://, and https:// schemes.
     *
     * @param url  the URL to convert
     * @param root the root path for relative path computation
     * @return Resource instance (PathResource for file/jar, UrlResource for http/https)
     */
    static Resource from(URL url, Path root) {
        String protocol = url.getProtocol();

        // Handle HTTP/HTTPS URLs by streaming content into UrlResource
        if ("http".equals(protocol) || "https".equals(protocol)) {
            try (java.io.InputStream is = url.openStream()) {
                byte[] bytes = FileUtils.toBytes(is);
                return root != null ? new UrlResource(url, bytes, root) : new UrlResource(url, bytes);
            } catch (Exception e) {
                throw new RuntimeException("Failed to fetch content from URL: " + url, e);
            }
        }

        // Handle file:// and jar:// URLs
        try {
            Path path = urlToPath(url, root);
            return root != null ? new PathResource(path, root) : new PathResource(path);
        } catch (java.nio.file.ProviderNotFoundException e) {
            // JAR file system provider not available (common in jpackage/JavaFX apps)
            // Fall back to streaming the resource content
            try (java.io.InputStream is = url.openStream()) {
                String content = FileUtils.toString(is);
                return root != null ? new MemoryResource(content, root) : new MemoryResource(content);
            } catch (Exception ex) {
                throw new RuntimeException("Failed to create resource from URL: " + url, ex);
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to create resource from URL: " + url, e);

View on GitHub (pinned to a22eb90246)