Issue
I try to use the java 11 HttpRequest
to call the msgraph webservice using the method PATCH:
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());
The error :
No signature of method: jdk.internal.net.http.HttpRequestBuilderImpl.PATCH() is applicable for argument types: (jdk.internal.net.http.RequestPublishers$StringPublisher) values: [jdk.internal.net.http.RequestPublishers$StringPublisher@280a600b]
the call itself works great, for instance in Postman. But I cannot make it work in groovy/java.
I used previously the HttpUrlConnection
but it does not support PATCH. Is it actually possible using HttpRequest
?
I could not find any working example of the use of the PATCH method on the Net.
Solution
According to docs, you can use "method" to specify other kind of methods like PATCH, OPTIONS, TRACE, etc.
In your case:
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.method("PATCH", jsonPayload)
.header("Content-Type", "application/json")
.build();
Answered By - z1lV3r
Answer Checked By - Mildred Charles (JavaFixing Admin)