我尝试使用 java 11HttpRequest
使用 PATCH 方法调用 msgraph webservice:
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.URI;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import groovy.json.JsonSlurper;
import groovy.json.JsonOutput;
access_token = "my_token";
def url = 'https://graph.microsoft.com/v1.0/groups/group_id/drive/items/01P4AIIJ5QTIIAZ2FLEZBIZWRV6KEBIMM5/workbook/worksheets/%7B00000000-0001-0000-0000-000000000000%7D/range(address=\'A1\')'
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_2)
.build();
jsonPayloadString = '{"values":["blabla"]}';
jsonPayload = HttpRequest.BodyPublishers.ofString(jsonPayloadString.toString())
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.PATCH(jsonPayload)
.header("Content-Type", "application/json")
.build();
HttpResponse response = httpClient.send(request,HttpResponse.BodyHandlers.ofString());
错误 :
方法没有签名:jdk.internal.net.http.HttpRequestBuilderImpl.PATCH() 适用于参数类型:(jdk.internal.net.http.RequestPublishers$StringPublisher) 值:[jdk.internal.net.http.RequestPublishers$ StringPublisher@280a600b]
调用本身效果很好,例如在 Postman 中。但我不能让它在 groovy/java 中工作。
我以前使用过,HttpUrlConnection
但它不支持PATCH。它实际上可以使用HttpRequest
吗?
我在网上找不到任何使用 PATCH 方法的工作示例。