@Override public void start() throws Exception { WebClient client = WebClient.create(vertx); client.get(8080, "localhost", "/").send(ar -> { if (ar.succeeded()) { HttpResponse<Buffer> response = ar.result(); System.out.println("Got HTTP response with status " + response.statusCode() + " with data " + response.body().toString("ISO-8859-1")); } else { ar.cause().printStackTrace(); } }); } }
@Override public void start() throws Exception { WebClient client = WebClient.create(vertx); client.get(8080, "localhost", "/") .as(BodyCodec.json(User.class)) .send(ar -> { if (ar.succeeded()) { HttpResponse<User> response = ar.result(); System.out.println("Got HTTP response body"); User user = response.body(); System.out.println("FirstName " + user.firstName); System.out.println("LastName " + user.lastName); System.out.println("Male " + user.male); } else { ar.cause().printStackTrace(); } }); } }
private void invoke(RoutingContext rc) { client.get("/luke").as(BodyCodec.string()).send(ar -> { if (ar.failed()) { rc.response().end("Unable to call the greeting service: " + ar.cause().getMessage()); } else { if (ar.result().statusCode() != 200) { rc.response().end("Unable to call the greeting service - received status:" + ar.result().statusMessage()); } else { rc.response().end("Greeting service invoked: '" + ar.result().body() + "'"); } } }); } }
@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(); } }); } }
JsonObject authJson = authHandler.result().body(); String accessToken = authJson.getString("access_token"); String header = "Bearer " + accessToken; .send(handler -> { if (handler.succeeded() && 200 == handler.result().statusCode()) { System.out.println(handler.result().body()); } else { System.out.println(handler.cause().getMessage());
/** * @return the response body in the format it was decoded. */ public T body() { if (cached_6 != null) { return cached_6; } T ret = (T)__typeArg_0.wrap(delegate.body()); cached_6 = ret; return ret; }
/** * @return the response body in the format it was decoded. */ public T body() { if (cached_6 != null) { return cached_6; } T ret = (T)__typeArg_0.wrap(delegate.body()); cached_6 = ret; return ret; }
public static java.lang.Object body(io.vertx.ext.web.client.HttpResponse<Object> j_receiver) { return io.vertx.core.impl.ConversionHelper.fromObject(j_receiver.body()); } public static java.util.Map<String, Object> bodyAsJsonObject(io.vertx.ext.web.client.HttpResponse<Object> j_receiver) {
private void getNow(RoutingContext ctx) { count++; final WebClient client = WebClient.create(vertx); client.get(80, HTTP_NOW, "/") .timeout(5000) .as(BodyCodec.jsonObject()) .send(ar -> { if (ar.succeeded()) { HttpResponse<JsonObject> response = ar.result(); JsonObject body = response.body(); String now = body.getJsonObject("now").getString("rfc2822"); ctx.response().end(now + " " + String.format(RESPONSE_STRING_FORMAT, HOSTNAME, count)); } else { ctx.response().setStatusCode(503).end(ar.cause().getMessage()); } }); }
private void callAuditService(RoutingContext context) { if (client == null) { context.response() .putHeader("content-type", "application/json") .setStatusCode(200) .end(new JsonObject().put("message", "No audit service").encode()); } else { client.get("/").send(ar -> { if (ar.succeeded()) { HttpResponse<Buffer> response = ar.result(); context.response() .putHeader("content-type", "application/json") .setStatusCode(200) .end(response.body()); } }); } } }
@Test public void testResponseBodyAsAsJsonObject() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonObject()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
@Test @DisplayName("Start a HTTP server, then issue a HTTP client request and check the response") void vertx_check_http_server_response() throws InterruptedException { Vertx vertx = Vertx.vertx(); VertxTestContext testContext = new VertxTestContext(); vertx.deployVerticle(new HttpServerVerticle(), testContext.succeeding(id -> { WebClient client = WebClient.create(vertx); client.get(8080, "localhost", "/") .as(BodyCodec.string()) .send(testContext.succeeding(response -> testContext.verify(() -> { assertThat(response.body()).isEqualTo("Plop"); testContext.completeNow(); }))); })); assertThat(testContext.awaitCompletion(5, TimeUnit.SECONDS)).isTrue(); closeVertx(vertx); }
@Test public void testResponseBodyAsAsJsonMapped() throws Exception { JsonObject expected = new JsonObject().put("cheese", "Goat Cheese").put("wine", "Condrieu"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.json(WineAndCheese.class)) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.body()); testComplete(); })); await(); }
@Test public void testResponseBodyAsBuffer() throws Exception { Buffer expected = TestUtils.randomBuffer(2000); server.requestHandler(req -> req.response().end(expected)); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
private <R> void testResponseMissingBody(BodyCodec<R> codec) throws Exception { server.requestHandler(req -> req.response().setStatusCode(403).end()); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(codec) .send(onSuccess(resp -> { assertEquals(403, resp.statusCode()); assertNull(resp.body()); testComplete(); })); await(); }
@Test public void testResponseBodyAsAsJsonArray() throws Exception { JsonArray expected = new JsonArray().add("cheese").add("wine"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.jsonArray()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.body()); testComplete(); })); await(); }
@Test public void testResponseBodyDiscarded() throws Exception { server.requestHandler(req -> req.response().end(TestUtils.randomAlphaString(1024))); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.none()) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(null, resp.body()); testComplete(); })); await(); }
@Test public void testResponseBodyAsAsJsonArrayMapped() throws Exception { JsonArray expected = new JsonArray().add("cheese").add("wine"); server.requestHandler(req -> req.response().end(expected.encode())); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get .as(BodyCodec.json(List.class)) .send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected.getList(), resp.body()); testComplete(); })); await(); }
@Test public void testInvalidRedirection() throws Exception { server.requestHandler(req -> { assertEquals(HttpMethod.POST, req.method()); assertEquals("/redirect", req.path()); req.response().setStatusCode(302).putHeader("Location", "http://www.google.com").end(); }); startServer(); HttpRequest<Buffer> builder = client .post("/redirect") .followRedirects(true); builder.send(onSuccess(resp -> { assertEquals(302, resp.statusCode()); assertEquals("http://www.google.com", resp.getHeader("Location")); assertNull(resp.body()); complete(); })); await(); }
@Test public void testRedirectLimit() throws Exception { String location = "http://" + DEFAULT_HTTP_HOST + ":" + DEFAULT_HTTP_PORT + "/redirect"; server.requestHandler(req -> { assertEquals(HttpMethod.GET, req.method()); assertEquals("/redirect", req.path()); req.response().setStatusCode(302).putHeader("Location", location).end(); }); startServer(); HttpRequest<Buffer> builder = client .get("/redirect") .followRedirects(true); builder.send(onSuccess(resp -> { assertEquals(302, resp.statusCode()); assertEquals(location, resp.getHeader("Location")); assertNull(resp.body()); complete(); })); await(); }