0

I want to get the body of a request. This request is HttpRequest. I'm using vertx. I try to get the body making a new webclient or I can use the existing routingcontext. (1)I don't know how to call the api with routing context.

(2)I'm using an HttpRequest to get the JsonObject. I then want to get the body of the Json. My code is below :

    void convertCurrency(String from, String to, String amount){
    HttpRequest<JsonObject> request = WebClient.create(vertx)
            .get(443, "https://api.apilayer.com/currency_data/convert?to="+to+"&from="+from+"&amount="+amount, "/")
            .ssl(true)  // (3)
            .putHeader("Accept", "application/json")
            .putHeader("apikey", "apikey")
            .as(BodyCodec.jsonObject())
            .expect(ResponsePredicate.SC_OK);

The first line gives me an error : java.lang.NullPointerException at io.vertx.ext.web.client.WebClient.create(WebClient.java:67)

Can you help me with problems (1) and (2)

Thank you

1 Answers1

0

To send and get the body from the response add

    request.send(new Handler<AsyncResult<HttpResponse<JsonObject>>>() {
        
        @Override
        public void handle(AsyncResult<HttpResponse<JsonObject>> ar) {
           if (ar.succeeded()) {
               ar.result().body();
           }
            
        }
    });

or its lamda equivalent

       request.send(ar -> {
            if (ar.succeeded()) {
                ar.result()
                  .body();
            }

        });
chhil
  • 442
  • 4
  • 12