无法通过JAVA在w REST API上执行PATCH

时间:2018-12-04 12:57:43

标签: java 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;

}

我正在使用带有参数的POST方法1和带有json正文的POST方法2

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

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

已解决:

HttpPost request = new HttpPost(requestURL);更改为HttpPatch request = new HttpPatch(requestURL);

我的网址(https)上也有问题,所以谢谢@Ivan Jadric