3

I'm working with Postman and I see that it has many modes. I was able to implement a restRequest object that "knows" how to send a request in Post or Get method.

This is part of my code:

 @Override
public RestResponse sendRequest() {
    return data.accept(new RequestDataVisitor<RestResponse>() {
        @Override
        public RestResponse visit(GetData getData) {
            return new RestResponse(webTarget.request().headers(headers).get());
        }

        @Override
        public RestResponse visit(PostFormData post) {
            return new RestResponse(webTarget.request(post.getMediaType()).headers(headers).post(post.getEntity()));
        }

        @Override
        public RestResponse visit(PostRawData post) {
            return new RestResponse(webTarget.request(post.getMediaType()).headers(headers).post(post.getEntity()));
        }

        @Override
        public RestResponse visit(DeleteData deleteData) {
            return new RestResponse(webTarget.request(deleteData.getMediaType()).headers(headers).delete());

        }
    });
}

How do I get my webTarget to send a request in Patch mode?

Tal Angel
  • 783
  • 1
  • 14
  • 38

2 Answers2

1

The issue was in WebTarget object:

WebTarget target = client.target(baseUrl).path(resourcePath)
.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND,"true");

PATCH method only works when SET_METHOD_WORKAROUND is true.

Tal Angel
  • 783
  • 1
  • 14
  • 38
0

You can use the method method with first argument set to "PATCH".

method is a generalized method (as opposed to post, get, delete which have the http-method hardwired) and allows you to call an arbitrary method:

webTarget.request(mediaType).headers(headers).method("PATCH", entity);

See the documentation for additional details: WebTarget.request gives you an instance of an Invocation.Builder which inherits method from SyncInvoker.

Note: depending on the JDK version and the JAX-RS library that you are using, you may run into problems when calling the PATCH method. If you do, see if any of these help:

mcernak
  • 8,230
  • 1
  • 3
  • 13