9

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.

Naman
  • 27,789
  • 26
  • 218
  • 353
laloune
  • 548
  • 1
  • 9
  • 26
  • 2
    `HttpRequest` uses `Builder` to build the request object, which has limited set of HTTP request types with build pattern, you can use `method` to support other request types. Check [this](https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpRequest.Builder.html) – NishanthSpShetty Nov 13 '19 at 17:37
  • For the line: `jsonPayload = HttpRequest.BodyPublishers.ofString(jsonPayloadString.toString())` isn't it `BodyPublisher` and not `BodyPublishers` as it says in https://docs.oracle.com/en/java/javase/11/docs/api/java.net.http/java/net/http/HttpRequest.BodyPublishers.html#ofString(java.lang.String)? – Bob the Builder Apr 06 '22 at 18:17

1 Answers1

22

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();
z1lV3r
  • 699
  • 7
  • 12
  • 1
    gee, thanks a ton! what I wonder is, why are GET() and POST() treated as a dedicated method and the others not... – laloune Nov 14 '19 at 11:58
  • Probably because the developer who wrote the class thoughts that other methods are more common to use. In my opinion PATCH was forgotten and it'll be added in other update. – z1lV3r Nov 19 '19 at 18:43