@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); client.put(8080, "localhost", "/", resp -> { System.out.println("Got response " + resp.statusCode()); resp.handler(buf -> System.out.println(buf.toString("UTF-8"))); }).setChunked(true).putHeader("Content-Type", "text/plain").write("hello").end(); } }
@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 Flowable<JsonObject> Flowable<JsonObject> obs1 = req1.toFlowable().flatMap(HttpClientResponse::toFlowable). map(buf -> new JsonObject(buf.toString("UTF-8"))); Flowable<JsonObject> obs2 = req2.toFlowable().flatMap(HttpClientResponse::toFlowable). 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); }, Throwable::printStackTrace); req1.end(); req2.end(); } }
private Observable<HttpClientResponse> get(HttpClient client, RequestOptions requestOptions, MultiMap headers) { return Observable.unsafeCreate(subscriber -> { HttpClientRequest req = client.get(requestOptions); req.headers().addAll(headers); if (headers.get(HttpHeaderNames.HOST.toString()) != null) { req.setHost(headers.get(HttpHeaderNames.HOST.toString())); } Observable<HttpClientResponse> resp = req.toObservable(); resp.subscribe(subscriber); req.end(); }); }
@Path("5") @GET public void hello5(@Suspended final AsyncResponse asyncResponse, // Inject the Vertx instance @Context Vertx vertx){ io.vertx.reactivex.core.Vertx rxVertx = io.vertx.reactivex.core.Vertx.newInstance(vertx); System.err.println("Creating client"); HttpClientOptions options = new HttpClientOptions(); options.setSsl(true); options.setTrustAll(true); options.setVerifyHost(false); io.vertx.reactivex.core.http.HttpClient client = rxVertx.createHttpClient(options); // DOES NOT WORK: https://github.com/vert-x3/vertx-rx/issues/13 Observable<io.vertx.reactivex.core.http.HttpClientResponse> responseHandler = client.get(443, "www.google.com", "/robots.txt").toObservable(); responseHandler.map(resp -> { System.err.println("Got response"); return resp.toObservable(); }) .subscribe(body -> { System.err.println("Got body"); asyncResponse.resume(Response.ok(body.toString()).build()); }); System.err.println("Created client"); }
private Observable<HttpClientResponse> get(HttpClient client, RequestOptions requestOptions, MultiMap headers) { return Observable.unsafeCreate(subscriber -> { HttpClientRequest req = client.get(requestOptions); req.headers().addAll(headers); if (headers.get(HttpHeaderNames.HOST.toString()) != null) { req.setHost(headers.get(HttpHeaderNames.HOST.toString())); } Observable<HttpClientResponse> resp = req.toObservable(); resp.subscribe(subscriber); req.end(); }); }
@Override public void start() throws Exception { HttpClient client = vertx.createHttpClient(); HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/"); req.toFlowable(). flatMap(HttpClientResponse::toFlowable) .map(buffer -> buffer.toJsonObject().mapTo(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.toFlowable(). // Status code check and -> Flowable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return resp.toFlowable(); }). 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.toFlowable(). // Status code check and -> Flowable<Buffer> flatMap(resp -> { if (resp.statusCode() != 200) { throw new RuntimeException("Wrong status code " + resp.statusCode()); } return Flowable.just(Buffer.buffer()).mergeWith(resp.toFlowable()); }). // 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(); } }