javahttphttp-headersvert.xweb-client

Using Vert.x Web Client to send GET requests properly with headers


I have an internal endpoint that I am trying to send GET requests to with Vert.x Web Client with Java. So far, I am unable to successfully get any data back.

If I cURL the endpoint, it works just fine (these are internal endpoints). The service I am trying to send GET requests to requires a few headers , and data as well:

curl -H "Accept:application/json" -H "Content-Type:application/json" -H "alpha:192.168.10.20" -d '{"mutate":"*"}' http://my-endpoint.com/api/get-items

But if I try to use this in one of my router endpoints in Vert.x, I get an error:

WebClient webClient = WebClient.create(vertx);
    webClient.get("http://my-endpoint.com/api/get-items")
        .putHeader("Accept", "application/json")
        .putHeader("Content-Type", "application/json")
        .putHeader("alpha", "192.168.10.20")
        .sendJsonObject(new JsonObject().put("mutate", "*"), ar -> { 
            if (ar.succeeded()) {
                System.out.println("##### WEBCLIENT #####");
                System.out.println(ar);
            } else {
                System.out.println("CAUSE: " + ar.cause().getMessage());

            }
    });

The error message I get from the else statement is:

CAUSE: Connection refused: localhost/127.0.0.1:80

What am I doing wrong? I've been using this for reference: Vert.x Web Client

===========================================

SOLUTION

===========================================

I had to change

webClient.get("http://my-endpoint.com/api/get-items")

to

webClient.post(80, "my-endpoint.com", "/api/get-items")

Also had to add .as(BodyCodec.jsonArray()) underneath the above line because the result I was getting was a Json Array.


Solution

  • You need to change

    webClient.get("http://my-endpoint.com/api/get-items")
    

    to

    webClient.get(80, "my-endpoint.com", "/api/get-items")