@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(); } }); } }
@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 { // 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(); } }); } }
/** * 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; }
/** * Create an HTTP GET request to send to the server at the default host and port. * @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 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(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 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> get(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.get(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 default host and port. * @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 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(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 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> get(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.get(port, host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
@Test public void testTimeout() throws Exception { AtomicInteger count = new AtomicInteger(); server.requestHandler(req -> count.incrementAndGet()); startServer(); HttpRequest<Buffer> get = client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); get.timeout(50).send(onFailure(err -> { assertTrue(err instanceof TimeoutException); testComplete(); })); await(); }
@Test public void testUserAgentHeaderRemoved() throws Exception { testRequest(client -> { HttpRequest<Buffer> request = client.get("somehost", "somepath"); request.headers().remove(HttpHeaders.USER_AGENT); return request; }, req -> assertEquals(Collections.emptyList(), req.headers().getAll(HttpHeaders.USER_AGENT))); }
@Test public void testVirtualHost() throws Exception { server.requestHandler(req -> { assertEquals("another-host:8080", req.host()); req.response().end(); }); startServer(); HttpRequest<Buffer> req = client.get("/test").virtualHost("another-host"); req.send(onSuccess(resp -> testComplete())); await(); }
@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))); }
@Test public void testBearerTokenAuthentication() throws Exception { testRequest( client -> client.get("somehost", "somepath").bearerTokenAuthentication("sometoken"), req -> { String auth = req.headers().get(HttpHeaders.AUTHORIZATION); assertEquals("Was expecting authorization header to contain a bearer token authentication string", "Bearer sometoken", auth); } ); }
@Test public void testBasicAuthentication() throws Exception { testRequest( client -> client.get("somehost", "somepath").basicAuthentication("ém$¨=!$€", "&@#§$*éà#\"'"), req -> { String auth = req.headers().get(HttpHeaders.AUTHORIZATION); assertEquals("Was expecting authorization header to contain a basic authentication string", "Basic w6ltJMKoPSEk4oKsLSZAI8KnJCrDqcOgIyIn", auth); } ); }
@Test public void testQueryParam() throws Exception { testRequest(client -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/").addQueryParam("param", "param_value"), req -> { assertEquals("param=param_value", req.query()); assertEquals("param_value", req.getParam("param")); }); }
@Test public void testOverwriteQueryParams() throws Exception { testRequest(client -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/?param=param_value1").setQueryParam("param", "param_value2"), req -> { assertEquals("param=param_value2", req.query()); assertEquals("param_value2", req.getParam("param")); }); }
@Test public void testQueryParamAppend() throws Exception { testRequest(client -> client.get(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/?param1=param1_value1").addQueryParam("param1", "param1_value2").addQueryParam("param2", "param2_value"), req -> { assertEquals("param1=param1_value1¶m1=param1_value2¶m2=param2_value", req.query()); assertEquals("param1_value1", req.getParam("param1")); assertEquals("param2_value", req.getParam("param2")); }); }