@Override public void start() throws Exception { WebClient client = WebClient.create(vertx); JsonObject user = new JsonObject() .put("firstName", "Dale") .put("lastName", "Cooper") .put("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(); } }); } }
@Override public void start() throws Exception { WebClient client = WebClient.create(vertx); Buffer body = Buffer.buffer("Hello World"); client.put(8080, "localhost", "/").sendBuffer(body, ar -> { if (ar.succeeded()) { HttpResponse<Buffer> response = ar.result(); System.out.println("Got HTTP response with status " + response.statusCode()); } else { ar.cause().printStackTrace(); } }); } }
@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); 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(); } }); } }
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", "/") .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(); } }); } }
@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", "/").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(); } }); } }
@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(); } }); }); }); } }
@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(); } }); } }
.send(authHandler -> { if (authHandler.succeeded() && 200 == authHandler.result().statusCode()) { JsonObject authJson = authHandler.result().body(); String accessToken = authJson.getString("access_token"); .putHeader("Authorization", header) .send(handler -> { if (handler.succeeded() && 200 == handler.result().statusCode()) { System.out.println(handler.result().body()); } else {
@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(); } }); } }
/** * @return the status code of the response */ public int statusCode() { if (cached_1 != null) { return cached_1; } int ret = delegate.statusCode(); cached_1 = ret; return ret; }
/** * @return the status code of the response */ public int statusCode() { if (cached_1 != null) { return cached_1; } int ret = delegate.statusCode(); cached_1 = ret; return ret; }
private void manageAuthResult(Handler<AsyncResult<Auth>> resultHandler, HttpResponse<Buffer> response) { if (response.statusCode() != 200) { resultHandler.handle(VaultException.toFailure(response.statusMessage(), response.statusCode(), response.bodyAsString())); } else { JsonObject object = response.bodyAsJsonObject(); Auth auth = object.getJsonObject("auth").mapTo(Auth.class); resultHandler.handle(Future.succeededFuture(auth)); } }
@Override public ConsulClient getKeysWithOptions(String keyPrefix, BlockingQueryOptions options, Handler<AsyncResult<List<String>>> resultHandler) { Query query = Query.of("recurse", true).put("keys", true).put(options); request(KV_VALID_CODES, HttpMethod.GET, "/v1/kv/" + urlEncode(keyPrefix), query, null, resultHandler, resp -> { if (resp.statusCode() == HttpResponseStatus.NOT_FOUND.code()) { return new ArrayList<>(); } else { return resp.bodyAsJsonArray().stream().map(Object::toString).collect(Collectors.toList()); } }); return this; }
@Override public ConsulClient getValueWithOptions(String key, BlockingQueryOptions options, Handler<AsyncResult<KeyValue>> resultHandler) { request(KV_VALID_CODES, HttpMethod.GET, "/v1/kv/" + urlEncode(key), new Query().put(options), null, resultHandler, resp -> { if (resp.statusCode() == HttpResponseStatus.NOT_FOUND.code()) { return new KeyValue(); } else { return KVParser.parse(resp.bodyAsJsonArray().getJsonObject(0)); } }); return this; }
@Test public void testResponseUnknownContentTypeBodyAsJsonObject() 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.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(expected, resp.bodyAsJsonObject()); testComplete(); })); await(); }
@Test public void testResponseUnknownContentTypeBodyAsJsonMapped() 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.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); assertEquals(new WineAndCheese().setCheese("Goat Cheese").setWine("Condrieu"), resp.bodyAsJson(WineAndCheese.class)); testComplete(); })); await(); }
@Test public void testMutateResponseInterceptor() throws Exception { server.requestHandler(req -> req.response().setStatusCode(500).end()); startServer(); client.addInterceptor(this::mutateResponseHandler); HttpRequest<Buffer> builder = client.get("/somepath"); builder.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); complete(); })); await(); }
@Test public void testCacheInterceptor() throws Exception { server.requestHandler(req -> fail()); startServer(); client.addInterceptor(this::cacheInterceptorHandler); HttpRequest<Buffer> builder = client.get("/somepath").host("localhost").port(8080); builder.send(onSuccess(resp -> { assertEquals(200, resp.statusCode()); complete(); })); await(); }