@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); 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 { 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(); } }); } }
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() + "'"); } } }); } }
@Test public void testCustomUserAgent() throws Exception { client = WebClient.wrap(super.client, new WebClientOptions().setUserAgent("smith")); testRequest(client -> client.get("somehost", "somepath"), req -> assertEquals(Collections.singletonList("smith"), req.headers().getAll(HttpHeaders.USER_AGENT))); }
/** * Create a web client using the provided <code>vertx</code> instance and default options. * @param vertx the vertx instance * @return the created web client */ public static io.vertx.rxjava.ext.web.client.WebClient create(io.vertx.rxjava.core.Vertx vertx) { io.vertx.rxjava.ext.web.client.WebClient ret = io.vertx.rxjava.ext.web.client.WebClient.newInstance(io.vertx.ext.web.client.WebClient.create(vertx.getDelegate())); return ret; }
/** * Close the client. Closing will close down any pooled connections. * Clients should always be closed after use. */ public void close() { delegate.close(); }
private void testTLS(WebClientOptions clientOptions, HttpServerOptions serverOptions, Function<WebClient, HttpRequest<Buffer>> requestProvider, Consumer<HttpServerRequest> serverAssertions) throws Exception { WebClient sslClient = WebClient.create(vertx, clientOptions); HttpServer sslServer = vertx.createHttpServer(serverOptions); sslServer.requestHandler(req -> { assertEquals(serverOptions.isSsl(), req.isSSL()); if (serverAssertions != null) { serverAssertions.accept(req); } req.response().end(); }); try { startServer(sslServer); HttpRequest<Buffer> builder = requestProvider.apply(sslClient); builder.send(onSuccess(resp -> testComplete())); await(); } finally { sslClient.close(); sslServer.close(); } }
private void testRequest(HttpMethod method) throws Exception { testRequest(client -> { switch (method) { case GET: return client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); case HEAD: return client.head(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); case DELETE: return client.delete(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); default: fail("Invalid HTTP method"); return null; } }, req -> assertEquals(method, req.method())); }
@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(); } }
/** * Wrap an <code>httpClient</code> with a web client and default options. * @param httpClient the to wrap * @return the web client */ public static io.vertx.rxjava.ext.web.client.WebClient wrap(io.vertx.rxjava.core.http.HttpClient httpClient) { io.vertx.rxjava.ext.web.client.WebClient ret = io.vertx.rxjava.ext.web.client.WebClient.newInstance(io.vertx.ext.web.client.WebClient.wrap(httpClient.getDelegate())); return ret; }
/** * Create an HTTP POST request to send to the server at the specified host and default port. * @param host the host * @param requestURI the relative URI * @return an HTTP client request object */ public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> post(String host, String requestURI) { io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.post(host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
/** * Create an HTTP request to send to the server at the specified host and port. * @param method the HTTP method * @param options the request options * @return an HTTP client request object */ public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> request(HttpMethod method, RequestOptions options) { io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.request(method, options), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
/** * Create an HTTP DELETE request to send to the server at the specified host and port. * @param port the port * @param host the host * @param requestURI the relative URI * @return an HTTP client request object */ public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> delete(int port, String host, String requestURI) { io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.delete(port, host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
/** * Create an HTTP GET request to send to the server at the specified host and default port. * @param host the host * @param requestURI the relative URI * @return an HTTP client request object */ public io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> get(String host, String requestURI) { io.vertx.rxjava.ext.web.client.HttpRequest<io.vertx.rxjava.core.buffer.Buffer> ret = io.vertx.rxjava.ext.web.client.HttpRequest.newInstance(delegate.get(host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
@Test public void testUserAgentDisabled() throws Exception { client = WebClient.wrap(super.client, new WebClientOptions().setUserAgentEnabled(false)); testRequest(client -> client.get("somehost", "somepath"), req -> assertEquals(Collections.emptyList(), req.headers().getAll(HttpHeaders.USER_AGENT))); }
/** * Create a web client using the provided <code>vertx</code> instance. * @param vertx the vertx instance * @param options the Web Client options * @return the created web client */ public static io.vertx.rxjava.ext.web.client.WebClient create(io.vertx.rxjava.core.Vertx vertx, WebClientOptions options) { io.vertx.rxjava.ext.web.client.WebClient ret = io.vertx.rxjava.ext.web.client.WebClient.newInstance(io.vertx.ext.web.client.WebClient.create(vertx.getDelegate(), options)); return ret; }
@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(); } }); } }
/** * Closes the client. */ public void close() { if (client != null) { client.close(); } }