{"record":{"id":"85a57daf13f72d42","repo":"didi/DoKit","slug":"expected-one-of-methods-but-was-method","errorCode":null,"errorMessage":"Expected one of \" + METHODS + \" but was \" + method","messagePattern":"Expected one of \" \\+ METHODS \\+ \" but was \" \\+ method","errorType":"exception","errorClass":"ProtocolException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java","lineNumber":725,"sourceCode":"        @Override\n        public void addRequestProperty(String field, String value) {\n            if (connected) {\n                throw new IllegalStateException(\"Cannot add request property after connection is made\");\n            }\n            if (field == null) {\n                throw new NullPointerException(\"field == null\");\n            }\n            if (value == null) {\n                return;\n            }\n\n            requestHeaders.add(field, value);\n        }\n\n        @Override\n        public void setRequestMethod(String method) throws ProtocolException {\n            if (!METHODS.contains(method)) {\n                throw new ProtocolException(\"Expected one of \" + METHODS + \" but was \" + method);\n            }\n            this.method = method;\n        }\n\n        @Override\n        public void setFixedLengthStreamingMode(int contentLength) {\n            setFixedLengthStreamingMode((long) contentLength);\n        }\n\n        @Override\n        public void setFixedLengthStreamingMode(long contentLength) {\n            if (super.connected) throw new IllegalStateException(\"Already connected\");\n            if (chunkLength > 0) throw new IllegalStateException(\"Already in chunked mode\");\n            if (contentLength < 0) throw new IllegalArgumentException(\"contentLength < 0\");\n            this.fixedContentLength = contentLength;\n            super.fixedContentLength = (int) Math.min(contentLength, Integer.MAX_VALUE);\n        }\n","sourceCodeStart":707,"sourceCodeEnd":743,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/aop/urlconnection/ObsoleteUrlFactory.java#L707-L743","documentation":"setRequestMethod() validates the method against a fixed METHODS set (the HTTP methods OkHttp's HttpURLConnection shim supports: GET, POST, HEAD, OPTIONS, PUT, DELETE, TRACE) and throws ProtocolException(\"Expected one of \" + METHODS + \" but was \" + method) for anything else. Custom methods like PATCH or PROPFIND are not accepted.","triggerScenarios":"setRequestMethod(\"PATCH\") — the classic case, since PATCH is not in the set; typos like \"post\" (lowercase) or \"GET \" (trailing space); custom WebDAV methods.","commonSituations":"Migrating a POST endpoint to PATCH; copy-pasted method strings with wrong case; REST wrappers exposing arbitrary verbs.","solutions":["For PATCH specifically, send POST with an X-HTTP-Method-Override: PATCH header, or use OkHttp's Request.Builder.method(\"PATCH\", body) directly which supports arbitrary methods.","Verify the method string: exact uppercase, no surrounding whitespace, matches one of GET/POST/HEAD/OPTIONS/PUT/DELETE/TRACE.","Constant-define method names instead of building them dynamically."],"exampleFix":"// before\nconn.setRequestMethod(\"PATCH\"); // ProtocolException\n\n// after\nconn.setRequestMethod(\"POST\");\nconn.setRequestProperty(\"X-HTTP-Method-Override\", \"PATCH\");\n// or use OkHttp directly:\n// Request req = new Request.Builder().url(u).method(\"PATCH\", body).build();","handlingStrategy":"validation","validationCode":"Set<String> OK = new HashSet<>(Arrays.asList(\"GET\",\"POST\",\"HEAD\",\"OPTIONS\",\"PUT\",\"DELETE\",\"TRACE\"));\nString m = method.trim().toUpperCase(Locale.US);\nif (!OK.contains(m)) { /* use OkHttp Request.Builder or POST + X-HTTP-Method-Override */ }","typeGuard":"static boolean isSupportedUrlConnectionMethod(String m) {\n  return Arrays.asList(\"GET\",\"POST\",\"HEAD\",\"OPTIONS\",\"PUT\",\"DELETE\",\"TRACE\").contains(m);\n}","tryCatchPattern":null,"preventionTips":["Remember PATCH is NOT supported via HttpURLConnection.setRequestMethod — plan for POST+override or OkHttp.","Normalize method strings to uppercase without whitespace before setting."],"tags":["network","http","urlconnection","http-method"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}