vert-x3 / vertx-web

HTTP web applications for Vert.x
Apache License 2.0
1.11k stars 535 forks source link

Issue with POST request hanging indefinitely in Vert.x Web #2597

Closed gianluca-valentini closed 7 months ago

gianluca-valentini commented 7 months ago

Hello,

I'm using Vert.x and Vert.x Web 4.5.0. After implementing a route that is correctly invoked, I'm unable to make a POST REST call to the server itself. The call hangs indefinitely. The POST call I'm making is a URL connection without setting any proxy:

URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); The call hangs at: int responseCode = conn.getResponseCode(); How can I achieve the desired call? Where am I going wrong? Is there a different way to make these internal calls?

consider that I need to have the internal rest response in order to complete the first route called

Thank you. Gianluca

tsegismont commented 7 months ago

This is most likely happening because the JDK HttpClient is a blocking client and so you're blocking the server event loop.

You could try to do the same with the Vert.x client. But, in general, a server which calls itself is not a recommended practice.

gianluca-valentini commented 7 months ago

Hi @tsegismont thanks for your answer. What would then be the best approach in these cases? If I use the HTTP client

WebClient clientVertx = WebClient.create(vertx); 
HttpRequest<Buffer> request = clientVertx.post(port, host,
V1ReSTRoutes.getRoute(/myroute/insert));
request.putHeader("Authorization", "Bearer ****");
request.putHeader("Accept", "application/json");
request.putHeader("Content-Length", String.valueOf(payload.length()));
request.sendBuffer(Buffer.buffer(payload));
Future<HttpResponse<Buffer>> future = request.send();

It works fine but the call is asynchronous so I can't get the response of the internal post to return to the caller. How to address this situation? Gianluca

tsegismont commented 7 months ago

Check out the Vert.x core documentation about Future chaining and composition.

Vert.x API are asynchronous, understanding how to work with futures is key.