/** * Perform an HTTP search query * @param indexes indexes names. If null search on all indexes * @param type document type separated by comma. If null search on all types * @param query json body query * @return elasticsearch response */ public Single<SearchResponse> search(final String indexes, final String type, final String query) { // index can be null _search on all index final StringBuilder url = new StringBuilder() .append('/') .append(indexes); if (type != null) { url.append('/').append(type); } url.append(URL_SEARCH); return httpClient .post(url.toString()) .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON) .rxSendBuffer(Buffer.buffer(query)) .map(response -> { if (response.statusCode() != HttpStatusCode.OK_200) { logger.error("Unable to search: url[{}] status[{}] query[{}] response[{}]", url.toString(), response.statusCode(), query, response.body()); throw new ElasticsearchException("Unable to search"); } return mapper.readValue(response.bodyAsString(), SearchResponse.class); }); }
httpRequest = webClient.post(443,request.getUrlParser().getHost(),Preconditions.isNotBlank(request.getUrlParser().getQuery())?request.getUrlParser().getPath()+"?"+request.getUrlParser().getQuery():request.getUrlParser().getPath()).ssl(true);
@Override public Single<BulkResponse> bulk(final List<String> data) { if (data != null && !data.isEmpty()) { String content = data.stream().collect(Collectors.joining()); return httpClient .post(URL_BULK) .putHeader(HttpHeaders.CONTENT_TYPE, "application/x-ndjson") .rxSendBuffer(Buffer.buffer(content)) .map(response -> { if (response.statusCode() != HttpStatusCode.OK_200) { logger.error("Unable to bulk index data: status[{}] data[{}] response[{}]", response.statusCode(), content, response.body()); throw new ElasticsearchException("Unable to bulk index data"); } return mapper.readValue(response.bodyAsString(), BulkResponse.class); }); } return Single.never(); }
/** * Example of method not using a circuit breaker. * * @param rc the routing context */ private void delegate(RoutingContext rc) { HttpEndpoint.rxGetWebClient(discovery, svc -> svc.getName().equals("currency-3rdparty-service")) .flatMap(client -> client.post("/").rxSendJsonObject(rc.getBodyAsJson())) .map(HttpResponse::bodyAsBuffer) .subscribe(toObserver(rc)); }
.put("client_secret", clientSecret) .put("code", code); HttpResponse<JsonObject> response = await(webClient.post(443, "github.com", "/login/oauth/access_token") .putHeader("Accept", "application/json") .putHeader("Content-Type", "application/json")
.put("client_secret", clientSecret) .put("code", code); HttpResponse<JsonObject> response = await(webClient.post(443, "github.com", "/login/oauth/access_token") .putHeader("Accept", "application/json") .putHeader("Content-Type", "application/json")
.setSsl(true) .setUserAgent("vert-x3")); HttpResponse<JsonObject> response = await(webClient.post(443, "api.github.com", "/gists") .putHeader("Accept", "application/vnd.github.v3+json") .putHeader("Content-Type", "application/json")
.setSsl(true) .setUserAgent("vert-x3")); HttpResponse<JsonObject> response = await(webClient.post(443, "api.github.com", "/gists") .putHeader("Accept", "application/vnd.github.v3+json") .putHeader("Content-Type", "application/json")
private void testPostRequest(VertxTestContext context, Vertx vertx, String url, Consumer<HttpResponse<Buffer>> expectedResponse) { WebClient client = WebClient.create(vertx); MultiMap formData = MultiMap.caseInsensitiveMultiMap(); formData.add("a", "b"); Single<HttpResponse<Buffer>> httpResponseSingle = client .post(KnotxServerRoutingTest.KNOTX_SERVER_PORT, KnotxServerRoutingTest.KNOTX_SERVER_ADDRESS, url) .rxSendForm(formData); subscribeToResult_shouldSucceed(context, httpResponseSingle, expectedResponse); }
@Test @KnotxApplyConfiguration("io/knotx/server/test-server-csrf.json") public void whenDoPostSecureWithCSRF_expectOK( VertxTestContext context, Vertx vertx) { createPassThroughKnot(vertx, "test-splitter"); createPassThroughKnot(vertx, "test-assembler"); createSimpleKnot(vertx, "some-knot", "test", null); MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value"); WebClient client = WebClient.create(vertx); client.get(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html").send( ar -> { if (ar.succeeded()) { String token = getToken(ar.result().cookies()); client.post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html") .putHeader(CSRFHandler.DEFAULT_HEADER_NAME, token) .putHeader(HttpHeaderNames.COOKIE.toString(), CSRFHandler.DEFAULT_COOKIE_NAME + "=" + token) .sendForm(body, res -> { if (res.succeeded()) { assertEquals(HttpResponseStatus.OK.code(), res.result().statusCode()); context.completeNow(); } else { context.failNow(ar.cause()); } }); } else { context.failNow(ar.cause()); } }); }
@Test @KnotxApplyConfiguration("io/knotx/server/test-server-csrf.json") public void whenDoPostSecureWithoutCSRF_expectForbidden( VertxTestContext context, Vertx vertx) { createPassThroughKnot(vertx, "test-splitter"); createPassThroughKnot(vertx, "test-assembler"); createSimpleKnot(vertx, "some-knot", "test", null); MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value"); WebClient client = WebClient.create(vertx); Single<HttpResponse<Buffer>> httpResponseSingle = client .post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/simple.html") .rxSendForm(body); subscribeToResult_shouldSucceed(context, httpResponseSingle, result -> { assertEquals(HttpResponseStatus.FORBIDDEN.code(), result.statusCode()); }); }
@Test @KnotxApplyConfiguration("io/knotx/server/test-server-csrf.json") public void whenDoPostPublicWithoutCSRF_expectOk( VertxTestContext context, Vertx vertx) { createPassThroughKnot(vertx, "test-splitter"); createPassThroughKnot(vertx, "test-assembler"); createSimpleKnot(vertx, "some-knot", "test", null); MultiMap body = MultiMap.caseInsensitiveMultiMap().add("field", "value"); WebClient client = WebClient.create(vertx); Single<HttpResponse<io.vertx.reactivex.core.buffer.Buffer>> httpResponseSingle = client .post(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, "/content/local/public.html") .rxSendForm(body); subscribeToResult_shouldSucceed(context, httpResponseSingle, resp -> { assertEquals(HttpResponseStatus.OK.code(), resp.statusCode()); }); }
@Test public void testPost() { int times = 5; waitFor(times); HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080)); server.requestStream().handler(req -> req.bodyHandler(buff -> { assertEquals("onetwothree", buff.toString()); req.response().end(); })); try { server.listen(ar -> { client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions())); Observable<Buffer> stream = Observable.just(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three")); Single<HttpResponse<Buffer>> single = client .post(8080, "localhost", "/the_uri") .rxSendStream(stream); for (int i = 0; i < times; i++) { single.subscribe(resp -> complete(), this::fail); } }); await(); } finally { server.close(); } }