首页 > 解决方案 > 无法通过 JAVA 对 w REST API 进行 PATCH

问题描述

我想用不同的方法调用 API:

使用 HttpClient 发布和获取都可以

我无法执行 PATCH 和 delete 方法,有没有人实现过这样的事情?如何 ?

发布方法1

public static String sendPost(String requestURL, Map<String, String> headers, String postParameters,
        boolean withProxy) throws IOException {

    HttpURLConnection con = createProxyHttpConnection(requestURL, withProxy);
    con.setRequestMethod("POST");
    con.setDoOutput(true);

    for (Map.Entry<String, String> entry : headers.entrySet()) {
        con.setRequestProperty(entry.getKey(), entry.getValue());

    }
    DataOutputStream wr = new DataOutputStream(con.getOutputStream());
    wr.writeBytes(postParameters);
    String response = IOUtils.toString(con.getInputStream(), "UTF-8");

    wr.close();
    con.disconnect();
    return response;

}

发布方法 2

public static HttpResponse sendPostBis(String requestURL, Map<String, String> headers, String payload,
        boolean withProxy) throws IOException {

    StringEntity sEntity = new StringEntity(payload,
            ContentType.APPLICATION_FORM_URLENCODED);

    HttpClient httpClient = HttpClientBuilder.create().build();
    HttpPost request = new HttpPost(requestURL);
    for (Map.Entry<String, String> entry : headers.entrySet()) {
        request.addHeader(entry.getKey(), entry.getValue());

    }
    request.setEntity(sEntity);

    HttpResponse response = httpClient.execute(request);
    return response;

}

我将方法 1 用于带有参数的 POST,将方法 2 用于带有 json body 的 POST

错误消息 (如果我在 SoapUI 中将方法更改为 POST 而不是 PATCH,我将收到相同的消息)

{"error":"No route found for \u0022POST \RESOURCE","message":"No route found for \u0022POST RESOURCE"}

标签: javarestapipatch

解决方案


在不提供任何代码的情况下,我最好的猜测是您尝试发出这些 PATCH 和 DELETE 请求的网络已经阻止了这些 HTTP 动词,因此您无法发出它们。大多数网络安全工具认为除 GET 和 POST 之外的任何动词都是不安全的,因此会将它们列入黑名单


推荐阅读