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());
}
});
}
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);
}
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();
});
}));
}
}));
}
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();
}
});
}
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();
}
});
});
});
}
Aggregations