Groovy脚本发送PATCH请求返回405错误,Postman请求正常
解决Groovy脚本发送PATCH请求返回405 Method Not Allowed的问题
我来帮你排查这个405错误的问题。从你的代码和运行日志来看,核心问题出在你用X-HTTP-Method-Override模拟PATCH请求的方式——你的目标服务器不认可这种方法,而Postman是直接发送原生PATCH请求,所以能正常返回200。
问题分析
- 请求模拟方式不被服务器认可:你的脚本里将请求方法设为POST,同时添加
X-HTTP-Method-Override: PATCH头来模拟PATCH请求,但很多Java后端服务(比如你的ServiceGesdenScim)默认不会识别这个自定义头,因此服务器会把它当成普通POST请求处理,而你的接口只支持PATCH方法,最终返回405。 - 不必要的兼容处理:Java 1.7及以上版本的
HttpURLConnection已经原生支持PATCH方法,完全不需要用POST来模拟。 - 代码小问题:代码里存在拼写错误
jsdonData(应为jsonData),还有一处潜在的NullPointerException(在inputLine初始化前就打印它)。
修正后的代码
直接使用原生PATCH方法发送请求,移除X-HTTP-Method-Override相关逻辑,同时修复其他小问题:
public Object sendHttpRequest(String url, String operation, String jsonData, String user, String password) throws Exception { println("Start sendHttpRequest() method"); Object gesdenResponse = null; HttpURLConnection conn = null; try { println("Opening HTTP connection..."); println("URL: " + url); URL obj = new URL(url); conn = (HttpURLConnection) obj.openConnection(); conn.setRequestProperty("Authorization", String.format("Basic %s", getProtectedCredentials(user, password))); println("Header \"Authorization: *****\" set up"); String method = null; switch (operation) { case "PASSWORD": method = "PATCH"; println("PASSWORD Operation"); break; default: break; } // 为需要发送请求体的方法开启输出流 if (["PUT", "POST", "PATCH"].contains(method)) { conn.setDoOutput(true); } // 直接设置原生PATCH请求方法 conn.setRequestMethod(method); println("Setting up custom HTTP headers..."); conn.setRequestProperty(GesdenConstants.HTTP_CUSTOM_HEADER_SYSTEM_KEY, GesdenConstants.HTTP_CUSTOM_HEADER_SYSTEM_VALUE); println(String.format("Custom header \"%s: %s\" set up", GesdenConstants.HTTP_CUSTOM_HEADER_SYSTEM_KEY, GesdenConstants.HTTP_CUSTOM_HEADER_SYSTEM_VALUE)); conn.setRequestProperty(GesdenConstants.HTTP_CUSTOM_HEADER_ACCEPT_KEY, GesdenConstants.HTTP_CUSTOM_HEADER_ACCEPT_VALUE); println(String.format("Custom header \"%s: %s\" set up", GesdenConstants.HTTP_CUSTOM_HEADER_ACCEPT_KEY, GesdenConstants.HTTP_CUSTOM_HEADER_ACCEPT_VALUE)); conn.setRequestProperty(GesdenConstants.HTTP_CUSTOM_HEADER_CONTENT_TYPE_KEY, GesdenConstants.HTTP_CUSTOM_HEADER_CONTENT_TYPE_VALUE); println(String.format("Custom header \"%s: %s\" set up", GesdenConstants.HTTP_CUSTOM_HEADER_CONTENT_TYPE_KEY, GesdenConstants.HTTP_CUSTOM_HEADER_CONTENT_TYPE_VALUE)); if (jsonData != null && !jsonData.isEmpty()) { // 让HttpURLConnection自动处理Content-Length,避免编码问题 OutputStream os = conn.getOutputStream(); os.write(jsonData.getBytes("UTF-8")); os.flush(); os.close(); println("JSON data sent successfully"); } println("Waiting for server response..."); println(String.format("conn is "+conn)); BufferedReader input = new BufferedReader(new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer data = new StringBuffer(); while ((inputLine = input.readLine()) != null) { data.append(inputLine); println(String.format("Response line: " +inputLine)); } input.close(); // 将响应内容赋值给返回对象 gesdenResponse = data.toString(); } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (conn != null) { conn.disconnect(); println("HTTP connection closed"); } println("Finish sendHttpRequest() method"); } return gesdenResponse; }
额外注意事项
- 确保你的Java版本在1.7及以上,这样
HttpURLConnection才能正确处理PATCH方法。 - 确认服务器端的
/Users/{user}/password端点确实支持PATCH方法(Postman能正常调用已经说明这点)。 - 修正后的代码移除了之前会导致
NullPointerException的提前打印inputLine的语句。
内容的提问来源于stack exchange,提问作者jfernandezmor
相关产品推荐
相关产品推荐

