@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); // Create two requests HttpClientRequest req1 = client.request(HttpMethod.GET, 8080, "localhost", "/"); HttpClientRequest req2 = client.request(HttpMethod.GET, 8080, "localhost", "/"); // Turn the requests responses into Observable<JsonObject> Observable<JsonObject> obs1 = req1.toObservable().flatMap(HttpClientResponse::toObservable). map(buf -> new JsonObject(buf.toString("UTF-8"))); Observable<JsonObject> obs2 = req2.toObservable().flatMap(HttpClientResponse::toObservable). map(buf -> new JsonObject(buf.toString("UTF-8"))); // Combine the responses with the zip into a single response obs1.zipWith(obs2, (b1, b2) -> new JsonObject().put("req1", b1).put("req2", b2)). subscribe(json -> { System.out.println("Got combined result " + json); }, err -> { err.printStackTrace(); }); req1.end(); req2.end(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). flatMap(HttpClientResponse::toObservable). // Unmarshall the response to the Data object via Jackon lift(io.vertx.rxjava.core.RxHelper.unmarshaller(Data.class)). subscribe( data -> { System.out.println("Got response " + data.message); }); // End request req.end(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return resp.toObservable(); }). subscribe(data -> System.out.println("Server content " + data.toString("UTF-8"))); // End request req.end(); } }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toObservable(). // Status code check and -> Observable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return Observable.just(Buffer.buffer()).mergeWith(resp.toObservable()); }). // Reduce all buffers in a single buffer reduce(Buffer::appendBuffer). // Turn in to a string map(buffer -> buffer.toString("UTF-8")). // Get a single buffer subscribe(data -> System.out.println("Server content " + data)); // End request req.end(); } }
onListen.subscribe( s -> { HttpClientRequest req = vertx.createHttpClient(new HttpClientOptions()).request(HttpMethod.PUT, 8080, "localhost", "/some/path", resp -> { }); req.putHeader("Content-Length", "3");