{"record":{"id":"97dcc6ed1a88b9c9","repo":"apache/shenyu","slug":"failed-to-get-swagger-document-http-status-code","errorCode":null,"errorMessage":"Failed to get Swagger document, HTTP status code: ","messagePattern":"Failed to get Swagger document, HTTP status code: ","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SwaggerImportServiceImpl.java","lineNumber":276,"sourceCode":"                return response.code() == 200;\n            }\n        } catch (Exception e) {\n            LOG.warn(\"Failed to test Swagger URL connection: {}\", swaggerUrl, e);\n            return false;\n        }\n    }\n    \n    private void validateSwaggerUrl(final String swaggerUrl) {\n        // Use UrlSecurityUtils for SSRF protection\n        UrlSecurityUtils.validateUrlForSSRF(swaggerUrl);\n    }\n    \n    private String fetchSwaggerDoc(final String swaggerUrl) throws IOException {\n        try (Response response = httpUtils.requestForResponse(swaggerUrl,\n                Collections.emptyMap(), Collections.emptyMap(), HttpUtils.HTTPMethod.GET, false)) {\n            \n            if (response.code() != 200) {\n                throw new RuntimeException(\"Failed to get Swagger document, HTTP status code: \" + response.code());\n            }\n\n            return HttpUtils.readLimitedResponseBody(response.body(), maxSwaggerBodySize);\n        }\n    }\n\n\n\n    private void validateSwaggerContent(final String swaggerJson) {\n        try {\n            JsonObject docRoot = GsonUtils.getInstance().fromJson(swaggerJson, JsonObject.class);\n            \n            // Detect version\n            boolean isV2 = docRoot.has(\"swagger\") && docRoot.get(\"swagger\").getAsString().startsWith(\"2.\");\n            boolean isV3 = docRoot.has(\"openapi\") && docRoot.get(\"openapi\").getAsString().startsWith(\"3.\");\n            \n            if (!isV2 && !isV3) {\n                throw new IllegalArgumentException(\"Unsupported Swagger version, only Swagger 2.0 and OpenAPI 3.0 formats are supported\");","sourceCodeStart":258,"sourceCodeEnd":294,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/SwaggerImportServiceImpl.java#L258-L294","documentation":"fetchSwaggerDoc() throws RuntimeException when the HTTP GET against the configured swaggerUrl returns any status other than 200. The message includes the actual status code so the caller can tell whether the endpoint is missing (404), unauthorized (401/403), or server-side broken (5xx).","triggerScenarios":"Swagger/MCP import with a swaggerUrl that responds non-200: wrong path, backend service not exposing /v2/api-docs or /v3/api-docs, missing auth, or a gateway/proxy returning an error page.","commonSituations":"Typo in the swagger URL; fetching an HTML login page instead of JSON (302 to login -> non-200 or wrong content); the backend app requires authentication for its docs endpoint; service is still starting and returns 503.","solutions":["Check the reported status code: 404 means wrong URL/path, 401/403 means auth is required, 5xx means backend failure.","curl the swaggerUrl from the admin server host to reproduce and inspect headers/body.","Correct the swaggerUrl to the actual docs endpoint (e.g. http://host:port/v3/api-docs).","Disable or satisfy auth requirements on the docs endpoint for the importing environment."],"exampleFix":"// before\nswaggerUrl: \"http://localhost:8080/api-docs\"   // 404\n// after\nswaggerUrl: \"http://localhost:8080/v3/api-docs\" // 200","handlingStrategy":"validation","validationCode":"Response r = client.newCall(new Request.Builder().url(swaggerUrl).build()).execute();\nif (r.code() != 200) throw new IllegalStateException(\"swagger endpoint returned \" + r.code() + \", fix URL/auth before import\");","typeGuard":null,"tryCatchPattern":"try {\n    service.importSwagger(request);\n} catch (RuntimeException e) {\n    if (e.getMessage() != null && e.getMessage().startsWith(\"Failed to get Swagger document, HTTP status code:\")) {\n        int code = Integer.parseInt(e.getMessage().replaceAll(\"\\\\D+\", \"\"));\n        // route by code: 404 -> fix URL, 401/403 -> fix auth, 5xx -> backend down\n    }\n    throw e;\n}","preventionTips":["Verify the exact docs endpoint path (/v2/api-docs vs /v3/api-docs) per framework version.","Disable auth on docs endpoints in internal environments, or supply credentials to the importer.","Curl the URL from the admin host before configuring it.","Watch for redirects to login pages — they often yield 401/403 or HTML instead of JSON."],"tags":["http","swagger","non-200","network"],"backgroundTag":"http-non-200-response","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}