@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(); } }); } }
/** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request <code>body</code> buffer. * @param body the body * @param handler */ public void sendBuffer(io.vertx.rxjava.core.buffer.Buffer body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendBuffer(body.getDelegate(), 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> buffer. * @param body the body * @param handler */ public void sendBuffer(io.vertx.rxjava.core.buffer.Buffer body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendBuffer(body.getDelegate(), 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())); } } }); }
rq.timeout(timeoutMs); rq.sendBuffer(body == null ? Buffer.buffer() : Buffer.buffer(body), h -> { if (h.succeeded()) { HttpResponse<Buffer> resp = h.result();
body = Buffer.buffer(); request.sendBuffer(body, InOut.replyWeb(this.getClass(), context, consumer));
body = Buffer.buffer(); request.sendBuffer(body, InOut.replyWeb(this.getClass(), context, consumer));
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(); }
@Test public void testStreamHttpServerRequest() throws Exception { Buffer expected = TestUtils.randomBuffer(10000); HttpServer server2 = vertx.createHttpServer(new HttpServerOptions().setPort(8081)).requestHandler(req -> req.bodyHandler(body -> { assertEquals(body, expected); req.response().end(); })); startServer(server2); WebClient webClient = WebClient.create(vertx); try { server.requestHandler(req -> webClient.postAbs("http://localhost:8081/") .sendStream(req, onSuccess(resp -> req.response().end("ok")))); startServer(); webClient.post(8080, "localhost", "/").sendBuffer(expected, onSuccess(resp -> { assertEquals("ok", resp.bodyAsString()); complete(); })); await(); } finally { server2.close(); } }
HttpRequest<Buffer> req = client.post("/"); req.send(handler); req.sendBuffer(Buffer.buffer(), handler); req.sendForm(new CaseInsensitiveHeaders().add("a", "b"), handler); req.sendJson("", handler);