{"record":{"id":"87880a8a1403a001","repo":"karatelabs/karate","slug":"client-credentials-auth-request-failed-e-getmessage","errorCode":null,"errorMessage":"client credentials auth request failed: \" + e.getMessage()","messagePattern":"client credentials auth request failed: \" \\+ e\\.getMessage\\(\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/http/ClientCredentialsAuthHandler.java","lineNumber":55,"sourceCode":"                    logger.warn(\"failed to parse main request URL for relative token URL resolution: {}\", e.getMessage());\n                }\n            }\n        }\n\n        builder.url(tokenUrl);\n        builder.formField(\"grant_type\", \"client_credentials\");\n        builder.formField(\"client_id\", config.get(\"client_id\"));\n        builder.formField(\"client_secret\", config.get(\"client_secret\"));\n        if (config.containsKey(\"scope\")) {\n            builder.formField(\"scope\", config.get(\"scope\"));\n        }\n        builder.header(\"Accept\", \"application/json\");\n        try {\n            HttpResponse response = builder.invoke(\"post\");\n            return Json.of(response.getBodyString()).asMap();\n        } catch (Exception e) {\n            logger.error(\"client credentials auth request failed: {}\", e.getMessage());\n            throw new RuntimeException(\"client credentials auth request failed: \" + e.getMessage(), e);\n        }\n    }\n\n    @Override\n    public void apply(HttpRequestBuilder builder) {\n        if (token == null) {\n            token = requestToken(builder.forkNewBuilder(), config, builder.getUrl());\n        }\n        builder.header(\"Authorization\", \"Bearer \" + token.get(\"access_token\"));\n    }\n\n    @Override\n    public String getType() {\n        return \"oauth2\";\n    }\n\n    @Override\n    public String toCurlPreview(String platform) {","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/http/ClientCredentialsAuthHandler.java#L37-L73","documentation":"ClientCredentialsAuthHandler.requestToken POSTs the client-credentials token request and parses the JSON response. Any exception during the HTTP call or JSON parsing is logged and rethrown as a RuntimeException with message 'client credentials auth request failed: <detail>'. It wraps the original exception as the cause.","triggerScenarios":"Calling apply(builder) on a ClientCredentialsAuthHandler when: the token endpoint URL is wrong or unreachable, the connection fails or times out, the server returns a non-JSON body, or Json.of(...) cannot parse the response.","commonSituations":"Misconfigured tokenUrl, network/DNS failures, OAuth server returning an error page (HTML) instead of JSON for bad client_id/secret, TLS certificate problems, proxy blocking the outbound call.","solutions":["Check the token endpoint URL configured on the handler is correct and reachable (curl it)","Verify client_id/client_secret are valid — an auth server often returns HTML/JSON error bodies that fail parsing","Inspect the wrapped cause (e.getCause()) and the logged 'client credentials auth request failed' detail","Confirm network/proxy/TLS settings allow outbound POST to the auth server"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"// before wiring the handler, verify the endpoint responds\n// curl -s -o /dev/null -w '%{http_code}' -X POST -d 'grant_type=client_credentials' $TOKEN_URL","typeGuard":null,"tryCatchPattern":"try { handler.apply(builder); } catch (RuntimeException e) { log.error(\"token request failed: {} / {}\", e.getMessage(), e.getCause()); }","preventionTips":["Validate the token endpoint URL and credentials with curl before runtime","Configure sane connect/read timeouts and a bounded retry with backoff","Check the auth server returns application/json; an HTML error page will fail parsing","Monitor cause chains (TLS, DNS, proxy) when failures occur"],"tags":["oauth2","http","network","client-credentials"],"backgroundTag":"oauth-token-exchange-failed","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"}