Search in sources :

Example 1 with WebClient

use of io.vertx.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    // Create the web client and enable SSL/TLS with a trust store
    WebClient client = WebClient.create(vertx, new WebClientOptions().setSsl(true).setTrustStoreOptions(new JksOptions().setPath("client-truststore.jks").setPassword("wibble")));
    client.get(8443, "localhost", "/").send(ar -> {
        if (ar.succeeded()) {
            HttpResponse<Buffer> response = ar.result();
            System.out.println("Got HTTP response with status " + response.statusCode());
        } else {
            ar.cause().printStackTrace();
        }
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebClientOptions(io.vertx.ext.web.client.WebClientOptions) JksOptions(io.vertx.core.net.JksOptions) WebClient(io.vertx.ext.web.client.WebClient)

Example 2 with WebClient

use of io.vertx.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class TwitterOAuthExample method start.

@Override
public void start() throws Exception {
    // Create the web client.
    WebClient client = WebClient.create(vertx);
    String queryToSearch = "vertx";
    // First we need to authenticate our call.
    String authHeader = "Basic " + B64_ENCODED_AUTH;
    client.postAbs(AUTH_URL).as(BodyCodec.jsonObject()).addQueryParam("grant_type", "client_credentials").putHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8").putHeader("Authorization", authHeader).send(authHandler -> {
        // Authentication successful.
        if (authHandler.succeeded() && 200 == authHandler.result().statusCode()) {
            JsonObject authJson = authHandler.result().body();
            String accessToken = authJson.getString("access_token");
            String header = "Bearer " + accessToken;
            // Making call to search tweets.
            client.getAbs(TWEET_SEARCH_URL).as(BodyCodec.jsonObject()).addQueryParam("q", queryToSearch).putHeader("Authorization", header).send(handler -> {
                if (handler.succeeded() && 200 == handler.result().statusCode()) {
                    System.out.println(handler.result().body());
                } else {
                    System.out.println(handler.cause().getMessage());
                }
            });
        } else {
            // Authentication failed
            System.out.println(authHandler.cause().getMessage());
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.ext.web.client.WebClient)

Example 3 with WebClient

use of io.vertx.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    WebClient client = WebClient.create(vertx);
    client.get(8080, "localhost", "/").addQueryParam("firstName", "Dale").addQueryParam("lastName", "Cooper").addQueryParam("male", "true").send(ar -> {
        if (ar.succeeded()) {
            HttpResponse<Buffer> response = ar.result();
            System.out.println("Got HTTP response with status " + response.statusCode());
        } else {
            ar.cause().printStackTrace();
        }
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebClient(io.vertx.ext.web.client.WebClient)

Example 4 with WebClient

use of io.vertx.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    WebClient client = WebClient.create(vertx);
    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("firstName", "Dale");
    form.add("lastName", "Cooper");
    form.add("male", "true");
    client.post(8080, "localhost", "/").putHeader("content-type", "multipart/form-data").sendForm(form, ar -> {
        if (ar.succeeded()) {
            HttpResponse<Buffer> response = ar.result();
            System.out.println("Got HTTP response with status " + response.statusCode());
        } else {
            ar.cause().printStackTrace();
        }
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) MultiMap(io.vertx.core.MultiMap) WebClient(io.vertx.ext.web.client.WebClient)

Example 5 with WebClient

use of io.vertx.ext.web.client.WebClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    WebClient client = WebClient.create(vertx);
    client.get(8080, "localhost", "/").as(BodyCodec.jsonObject()).send(ar -> {
        if (ar.succeeded()) {
            HttpResponse<JsonObject> response = ar.result();
            System.out.println("Got HTTP response body");
            System.out.println(response.body().encodePrettily());
        } else {
            ar.cause().printStackTrace();
        }
    });
}
Also used : JsonObject(io.vertx.core.json.JsonObject) WebClient(io.vertx.ext.web.client.WebClient)

Aggregations

WebClient (io.vertx.ext.web.client.WebClient)16 Buffer (io.vertx.core.buffer.Buffer)11 JsonObject (io.vertx.core.json.JsonObject)3 MultiMap (io.vertx.core.MultiMap)2 Vertx (io.vertx.core.Vertx)2 VertxOptions (io.vertx.core.VertxOptions)2 WebClientOptions (io.vertx.ext.web.client.WebClientOptions)2 Future (io.vertx.core.Future)1 FileProps (io.vertx.core.file.FileProps)1 FileSystem (io.vertx.core.file.FileSystem)1 OpenOptions (io.vertx.core.file.OpenOptions)1 JksOptions (io.vertx.core.net.JksOptions)1 Async (io.vertx.ext.unit.Async)1 TestContext (io.vertx.ext.unit.TestContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 HttpResponse (io.vertx.ext.web.client.HttpResponse)1 BodyCodec (io.vertx.ext.web.codec.BodyCodec)1 Checkpoint (io.vertx.junit5.Checkpoint)1 VertxExtension (io.vertx.junit5.VertxExtension)1 VertxTestContext (io.vertx.junit5.VertxTestContext)1