vert-x3 / vertx-4-migration-guide

Migration to Vert.x 4 guide
https://vert-x3.github.io/vertx-4-migration-guide/index.html
20 stars 16 forks source link

io.vertx.core.http.HttpClient.postAbs or requestAbs methods are removed in Vertx 4.X.X. Which methods we should use now and how? #72

Closed sac10nikam closed 2 years ago

sac10nikam commented 2 years ago

Questions

io.vertx.core.http.HttpClient.postAbs or requestAbs methods are removed in Vertx 4.X.X. Which methods we should use now and how?

Version

4.3.2

Context

Migrating Vertx 3.9.7 to 4.3.2

Do you have a reproducer?

Yes

Steps to reproduce

NA

Extra

JVM 17 - Amazon Corrento

sac10nikam commented 2 years ago

@pendula95 Do you know this?

pendula95 commented 2 years ago

There was a lot of cleaning work for HttpClient. Most of the methods were removed for simplicity of API changes-in-http-client

If you want to have a http client for application business I would recommend using WebClient instead of HttpClient. As stated in migration guide:

Vert.x web client

Use the Vert.x web client when your applications are web oriented. For example, REST, encoding and decoding HTTP payloads, interpreting the HTTP status response code, and so on.

Vert.x HTTP client

Use the Vert.x HTTP client when your applications are used as HTTP proxy. For example, as an API gateway. The HTTP client has been updated and improved in Vert.x 4.

It is possible to get same behavior as before so here are examples: This is equivalent to using HttpClient.postAbs("http://enw5gif6c6lhk.x.pipedream.net/test", .....)

httpClient.request(HttpMethod.POST, 80, "enw5gif6c6lhk.x.pipedream.net", "/test", handler1 -> {
            if (handler1.succeeded()) {
                handler1.result().send(handler2 -> {
                    if (handler2.succeeded()) {
                        System.out.println(handler2.result().statusCode());
                        //System.out.println(handler2.result().body().succeeded());
                    } else {
                        handler2.cause().printStackTrace();
                    }
                });
            } else {
                handler1.cause().printStackTrace();
            }
        });

I would recommend using the Futures API as it is much cleaner:

httpClient.request(HttpMethod.POST, 80, "enw5gif6c6lhk.x.pipedream.net", "/test")
                .compose(HttpClientRequest::send)
                .onFailure(Throwable::printStackTrace)
                .onSuccess(response -> System.out.println(response.statusCode()));

Also here is an example using WebClient which has postAbs:

webClient.postAbs("http://enw5gif6c6lhk.x.pipedream.net/test")
                .send()
                .onFailure(Throwable::printStackTrace)
                .onSuccess(response -> System.out.println(response.statusCode()));

You are not the only one who asked this question so it might be a good idea to make a contribution to migration guide, so if you are up for it please take a shoot, especially if you are going to use this as you will get experience from first hand.

sac10nikam commented 2 years ago

@pendula95 Thanks your quick reply. I appreciate it.

sac10nikam commented 2 years ago

@pendula95 How can we migrate io.vertx.core.http.HttpClient.requestAbs in Vertx 4.3.2?

I could see, io.vertx.core.http.HttpClient.requestAbs overloaded methods has been moved to WebClient API. Not sure if I am pointing out correct.

Can we use requestAbs method from Webclient? Reference: https://vertx.io/docs/apidocs/io/vertx/ext/web/client/WebClient.html

vietj commented 2 years ago

you can use new RequestOptions().setAbsoluteURI(...) and make you request with

sac10nikam commented 2 years ago

@vietj Thanks. Let me check. Can you share its example?

pendula95 commented 2 years ago
HttpClient httpClient = vertx.createHttpClient();
httpClient.request(new RequestOptions().setAbsoluteURI("https://enpc7phb98di.x.pipedream.net")
    .setTimeout(3000))
  .compose(HttpClientRequest::send)
  .onFailure(Throwable::printStackTrace)
  .onSuccess(response -> System.out.println(response.statusCode()));
sac10nikam commented 2 years ago

Thanks @pendula95