{"record":{"id":"0b37babfdf17cf89","repo":"elastic/elasticsearch","slug":"with-body-is-not-supported","errorCode":null,"errorMessage":"{} with body is not supported","messagePattern":"(.+?) with body is not supported","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"client/rest/src/main/java/org/elasticsearch/client/RestClient.java","lineNumber":632,"sourceCode":"                return httpPost;\n            case HttpPut.METHOD_NAME:\n                return addRequestBody(new HttpPut(uri), entity, compressionEnabled);\n            case HttpTrace.METHOD_NAME:\n                return addRequestBody(new HttpTrace(uri), entity, compressionEnabled);\n            default:\n                throw new UnsupportedOperationException(\"http method not supported: \" + method);\n        }\n    }\n\n    private static HttpRequestBase addRequestBody(HttpRequestBase httpRequest, HttpEntity entity, boolean compressionEnabled) {\n        if (entity != null) {\n            if (httpRequest instanceof HttpEntityEnclosingRequestBase) {\n                if (compressionEnabled) {\n                    entity = new ContentCompressingEntity(entity);\n                }\n                ((HttpEntityEnclosingRequestBase) httpRequest).setEntity(entity);\n            } else {\n                throw new UnsupportedOperationException(httpRequest.getMethod() + \" with body is not supported\");\n            }\n        }\n        return httpRequest;\n    }\n\n    static URI buildUri(String pathPrefix, String path, Map<String, String> params) {\n        Objects.requireNonNull(path, \"path must not be null\");\n        try {\n            String fullPath;\n            if (pathPrefix != null && pathPrefix.isEmpty() == false) {\n                if (pathPrefix.endsWith(\"/\") && path.startsWith(\"/\")) {\n                    fullPath = pathPrefix.substring(0, pathPrefix.length() - 1) + path;\n                } else if (pathPrefix.endsWith(\"/\") || path.startsWith(\"/\")) {\n                    fullPath = pathPrefix + path;\n                } else {\n                    fullPath = pathPrefix + \"/\" + path;\n                }\n            } else {","sourceCodeStart":614,"sourceCodeEnd":650,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/client/rest/src/main/java/org/elasticsearch/client/RestClient.java#L614-L650","documentation":"Thrown by addRequestBody when a request body/entity is supplied for an HTTP method whose request type is not HttpEntityEnclosingRequestBase (i.e. not POST/PUT/PATCH/OPTIONS/TRACE). Those verbs (GET, DELETE, HEAD) never carry a body in Apache HttpClient, so the client refuses to attach one.","triggerScenarios":"Calling request.setEntity(...) or building a Request with a JSON body for a GET, DELETE or HEAD. Also reached indirectly via low-level helpers that always pass an entity.","commonSituations":"Sending a GET with a body (some APIs allow it, Elasticsearch generally does not); DELETE-with-body patterns ported from other HTTP clients; mistakenly attaching a body to a HEAD index-exists request.","solutions":["For GET/HEAD/DELETE, move parameters into the query string or endpoint path instead of a body.","If you genuinely need a body, switch the method to POST (e.g. use POST _search with the query body).","Audit request.setEntity / Request.setJsonEntity calls adjacent to GET/DELETE/HEAD constructors."],"exampleFix":"// before\nRequest req = new Request(\"GET\", \"/index/_search\");\nreq.setJsonEntity(\"{\\\"query\\\":{\\\"match_all\\\":{}}}\");\n// after\nRequest req = new Request(\"POST\", \"/index/_search\");\nreq.setJsonEntity(\"{\\\"query\\\":{\\\"match_all\\\":{}}}\");","handlingStrategy":"validation","validationCode":"private static final Set<String> BODY_OK = Set.of(\"POST\",\"PUT\",\"PATCH\",\"OPTIONS\",\"TRACE\");\nif (entity != null && !BODY_OK.contains(method)) throw new IllegalArgumentException(method + \" must not carry a body\");\nRequest req = new Request(method, endpoint);\nif (entity != null) req.setJsonEntity(entity);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat GET/HEAD/DELETE as body-less by construction in your wrapper.","Route search-with-body through POST _search instead of GET."],"tags":["rest-client","http-method","request-body"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T12:31:55.035Z"}