{"record":{"id":"510381b5af38a572","repo":"didi/DoKit","slug":"method-does-not-support-a-request-body-method","errorCode":null,"errorMessage":"method does not support a request body: ${method}","messagePattern":"method does not support a request body: (.+?)","errorType":"exception","errorClass":"ProtocolException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java","lineNumber":476,"sourceCode":"            Response response = getResponse(false);\n            if (response.code() >= HTTP_BAD_REQUEST) {\n                Log.e(\"DoKit\", \"FileNotFoundException:\" + url.toString());\n                return ConvertUtils.string2InputStream(\"FileNotFoundException:\" + url.toString(), \"utf-8\");\n            }\n\n            if (response.body() != null) {\n                return response.body().byteStream();\n            } else {\n                return ConvertUtils.string2InputStream(\"response.body() is null:\" + url.toString(), \"utf-8\");\n            }\n\n        }\n\n        @Override\n        public OutputStream getOutputStream() throws IOException {\n            OutputStreamRequestBody requestBody = (OutputStreamRequestBody) buildCall().request().body();\n            if (requestBody == null) {\n                throw new ProtocolException(\"method does not support a request body: \" + method);\n            }\n\n            if (requestBody instanceof StreamedRequestBody) {\n                connect();\n                networkInterceptor.proceed();\n            }\n\n            if (requestBody.closed) {\n                throw new ProtocolException(\"cannot write request body after response has been read\");\n            }\n\n            return requestBody.outputStream;\n        }\n\n        @Override\n        public Permission getPermission() {\n            URL url = getURL();\n            String hostname = url.getHost();","sourceCodeStart":458,"sourceCodeEnd":494,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java#L458-L494","documentation":"getOutputStream() builds the OkHttp call and inspects its request body; when buildCall() produced no OutputStreamRequestBody (because the method does not permit a body, e.g. GET without the doOutput promotion or methods like DELETE/HEAD), it throws ProtocolException(\"method does not support a request body: \" + method). Per the source, only doOutput=true turns GET into POST; other bodyless methods are not auto-promoted.","triggerScenarios":"Calling getOutputStream() after setRequestMethod(\"HEAD\") or \"DELETE\" with setDoOutput(true); calling getOutputStream() without setDoOutput(true) on a method that permits no body.","commonSituations":"Generic HTTP helper that always writes a body but lets callers pick arbitrary methods; switching a request from POST to DELETE/HEAD without removing the body-writing branch.","solutions":["Call setDoOutput(true) and use a body-permitting method (POST/PUT/PATCH) before getOutputStream().","For GET specifically, rely on the automatic GET->POST promotion by setting doOutput(true), or switch the method explicitly.","Skip body writing entirely for HEAD/DELETE-style calls; send DELETE parameters in the URL if the server allows."],"exampleFix":"// before\nconn.setRequestMethod(\"DELETE\");\nconn.setDoOutput(true);\nconn.getOutputStream().write(body); // ProtocolException\n\n// after\nconn.setRequestMethod(\"POST\");\nconn.setDoOutput(true);\nconn.getOutputStream().write(body);","handlingStrategy":"validation","validationCode":"static boolean permitsBody(String m) {\n  return m.equals(\"POST\") || m.equals(\"PUT\") || m.equals(\"PATCH\");\n}\nif (permitsBody(method)) { conn.setDoOutput(true); os = conn.getOutputStream(); }","typeGuard":"static boolean canWriteBody(HttpURLConnection c) {\n  String m = c.getRequestMethod();\n  return c.getDoOutput() && (m.equals(\"GET\") || permitsBody(m)); // GET auto-promotes to POST\n}","tryCatchPattern":null,"preventionTips":["Only request an OutputStream when the method carries a body.","Remember GET+doOutput is silently promoted to POST; other bodyless methods are not."],"tags":["network","http","urlconnection","request-body"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}