@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(); } }); } }
private void testSendBody(Object body, BiConsumer<String, Buffer> checker) throws Exception { waitFor(2); server.requestHandler(req -> req.bodyHandler(buff -> { checker.accept(req.getHeader("content-type"), buff); complete(); req.response().end(); })); startServer(); HttpRequest<Buffer> post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); if (body instanceof Buffer) { post.sendBuffer((Buffer) body, onSuccess(resp -> complete())); } else if (body instanceof JsonObject) { post.sendJsonObject((JsonObject) body, onSuccess(resp -> complete())); } else { post.sendJson(body, onSuccess(resp -> complete())); } await(); }
@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(); } }); } }
req.sendBuffer(Buffer.buffer(), handler); req.sendForm(new CaseInsensitiveHeaders().add("a", "b"), handler); req.sendJson("", handler); req.sendJsonObject(new JsonObject(), handler); req.sendMultipartForm(MultipartForm.create().attribute("a", "b"), handler);
/** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request <code>body</code> object encoded as json and the content type * set to <code>application/json</code>. * @param body the body * @param handler */ public void sendJson(Object body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendJson(body, new Handler<AsyncResult<io.vertx.ext.web.client.HttpResponse<T>>>() { public void handle(AsyncResult<io.vertx.ext.web.client.HttpResponse<T>> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance(ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
/** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request <code>body</code> object encoded as json and the content type * set to <code>application/json</code>. * @param body the body * @param handler */ public void sendJson(Object body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendJson(body, new Handler<AsyncResult<io.vertx.ext.web.client.HttpResponse<T>>>() { public void handle(AsyncResult<io.vertx.ext.web.client.HttpResponse<T>> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance(ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
public static void sendJson(io.vertx.ext.web.client.HttpRequest<Object> j_receiver, java.lang.Object body, io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.ext.web.client.HttpResponse<java.lang.Object>>> handler) { j_receiver.sendJson(io.vertx.core.impl.ConversionHelper.toObject(body), handler != null ? new io.vertx.core.Handler<io.vertx.core.AsyncResult<io.vertx.ext.web.client.HttpResponse<java.lang.Object>>>() { public void handle(io.vertx.core.AsyncResult<io.vertx.ext.web.client.HttpResponse<java.lang.Object>> ar) { handler.handle(ar.map(event -> io.vertx.core.impl.ConversionHelper.fromObject(event))); } } : null); } }
/** * Get all info about messaging client (stdOut, stdErr, code, isRunning) * * @param uuid client id * @return * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException */ public JsonObject getClientInfo(String uuid) throws InterruptedException, ExecutionException, TimeoutException { CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>(); JsonObject request = new JsonObject(); request.put("id", uuid); client.get(endpoint.getPort(), endpoint.getHost(), "") .as(BodyCodec.jsonObject()) .timeout(120000) .sendJson(request, ar -> responseHandler(ar, responsePromise, HttpURLConnection.HTTP_OK, "Error getting messaging clients info")); return responsePromise.get(150000, TimeUnit.SECONDS); }
/** * Stop messaging client and get all informations about them (stdOut, stdErr, code, isRunning) * * @param uuid client id * @return * @throws InterruptedException * @throws ExecutionException * @throws TimeoutException */ public JsonObject stopClient(String uuid) throws InterruptedException, ExecutionException, TimeoutException { CompletableFuture<JsonObject> responsePromise = new CompletableFuture<>(); JsonObject request = new JsonObject(); request.put("id", uuid); client.delete(endpoint.getPort(), endpoint.getHost(), "") .as(BodyCodec.jsonObject()) .timeout(120000) .sendJson(request, ar -> responseHandler(ar, responsePromise, HttpURLConnection.HTTP_OK, "Error removing messaging clients")); return responsePromise.get(150000, TimeUnit.SECONDS); }