Search in sources :

Example 6 with WebClient

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

the class WebClientExamples method simpleGetAndHead.

public void simpleGetAndHead(Vertx vertx) {
    WebClient client = WebClient.create(vertx);
    // Send a GET request
    client.get(8080, "myserver.mycompany.com", "/some-uri").send(ar -> {
        if (ar.succeeded()) {
            // Obtain response
            HttpResponse<Buffer> response = ar.result();
            System.out.println("Received response with status code" + response.statusCode());
        } else {
            System.out.println("Something went wrong " + ar.cause().getMessage());
        }
    });
    // Send a HEAD request
    client.head(8080, "myserver.mycompany.com", "/some-uri").send(ar -> {
        if (ar.succeeded()) {
            // Obtain response
            HttpResponse<Buffer> response = ar.result();
            System.out.println("Received response with status code" + response.statusCode());
        } else {
            System.out.println("Something went wrong " + ar.cause().getMessage());
        }
    });
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WebClient(io.vertx.ext.web.client.WebClient)

Example 7 with WebClient

use of io.vertx.ext.web.client.WebClient in project vertx-swagger by bobxwang.

the class IndexVerticleTest method TestIndex.

@Test
public void TestIndex(TestContext context) {
    Async async = context.async();
    VertxOptions vertxOptions = new VertxOptions();
    vertxOptions.setMaxEventLoopExecuteTime(Long.MAX_VALUE);
    vertxOptions.setMaxWorkerExecuteTime(Long.MAX_VALUE);
    final Vertx vertx = Vertx.vertx(vertxOptions);
    vertx.createHttpServer().requestHandler(req -> req.response().putHeader("Content-Type", "text/plain").end("OK")).listen(8080, context.asyncAssertSuccess(s -> {
        WebClient webClient = WebClient.create(vertx);
        webClient.get(8080, "localhost", "/").send(ar -> {
            if (ar.succeeded()) {
                HttpResponse<Buffer> response = ar.result();
                context.assertTrue(response.headers().contains("Content-Type"));
                context.assertEquals("text/plain", response.getHeader("Content-Type"));
                context.assertEquals("OK", response.bodyAsString());
                webClient.close();
                async.complete();
            } else {
                async.resolve(Future.failedFuture(ar.cause()));
            }
        });
    }));
    async.awaitSuccess(5000);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) HttpResponse(io.vertx.ext.web.client.HttpResponse) Buffer(io.vertx.core.buffer.Buffer) WebClient(io.vertx.ext.web.client.WebClient) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) VertxOptions(io.vertx.core.VertxOptions) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Future(io.vertx.core.Future) Async(io.vertx.ext.unit.Async) HttpResponse(io.vertx.ext.web.client.HttpResponse) Vertx(io.vertx.core.Vertx) VertxOptions(io.vertx.core.VertxOptions) WebClient(io.vertx.ext.web.client.WebClient) Test(org.junit.Test)

Example 8 with WebClient

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

the class SampleVerticleTest method useSampleVerticle.

@Test
@DisplayName("🚀 Deploy a HTTP service verticle and make 10 requests")
void useSampleVerticle(Vertx vertx, VertxTestContext testContext) {
    WebClient webClient = WebClient.create(vertx);
    Checkpoint deploymentCheckpoint = testContext.checkpoint();
    Checkpoint requestCheckpoint = testContext.checkpoint(10);
    vertx.deployVerticle(new SampleVerticle(), testContext.succeeding(id -> {
        deploymentCheckpoint.flag();
        for (int i = 0; i < 10; i++) {
            webClient.get(11981, "localhost", "/").as(BodyCodec.string()).send(testContext.succeeding(resp -> {
                testContext.verify(() -> {
                    assertThat(resp.statusCode()).isEqualTo(200);
                    assertThat(resp.body()).contains("Yo!");
                    requestCheckpoint.flag();
                });
            }));
        }
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) WebClient(io.vertx.ext.web.client.WebClient) org.junit.jupiter.api(org.junit.jupiter.api) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) BodyCodec(io.vertx.ext.web.codec.BodyCodec) VertxOptions(io.vertx.core.VertxOptions) Assertions(org.assertj.core.api.Assertions) Checkpoint(io.vertx.junit5.Checkpoint) VertxExtension(io.vertx.junit5.VertxExtension) Checkpoint(io.vertx.junit5.Checkpoint) WebClient(io.vertx.ext.web.client.WebClient)

Example 9 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);
    User user = new User();
    user.firstName = "Dale";
    user.lastName = "Cooper";
    user.male = true;
    client.put(8080, "localhost", "/").sendJson(user, 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 10 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 {
    String filename = "upload.txt";
    FileSystem fs = vertx.fileSystem();
    WebClient client = WebClient.create(vertx);
    fs.props(filename, ares -> {
        FileProps props = ares.result();
        System.out.println("props is " + props);
        long size = props.size();
        HttpRequest<Buffer> req = client.put(8080, "localhost", "/");
        req.putHeader("content-length", "" + size);
        fs.open(filename, new OpenOptions(), ares2 -> {
            req.sendStream(ares2.result(), 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) OpenOptions(io.vertx.core.file.OpenOptions) FileSystem(io.vertx.core.file.FileSystem) WebClient(io.vertx.ext.web.client.WebClient) FileProps(io.vertx.core.file.FileProps)

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