@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(); } }); } }
@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(); } }
@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", "/") .putHeader("content-type", "multipart/form-data") .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(); } }); } }
/** * Create an HTTP POST 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> post(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(requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
/** * Create an HTTP POST 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> post(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.post(port, host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
/** * Create an HTTP POST 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> post(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.post(port, host, requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); 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 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 POST 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> post(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(requestURI), io.vertx.rxjava.core.buffer.Buffer.__TYPE_ARG); return ret; }
/** * Logs in against the `Cert` backend. Certificates are configured directly on the client instance. * * @param resultHandler the callback invoked with the result */ public void loginWithCert(Handler<AsyncResult<Auth>> resultHandler) { client.post("/v1/auth/cert/login") .send(ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }); }
/** * Logs in against the `AppRole` backend. * * @param roleId the role id * @param secretId the secret id * @param resultHandler the callback invoked with the result */ public void loginWithAppRole(String roleId, String secretId, Handler<AsyncResult<Auth>> resultHandler) { JsonObject payload = new JsonObject() .put("role_id", Objects.requireNonNull(roleId, "The role must not be null")) .put("secret_id", Objects.requireNonNull(secretId, "The secret must not be null")); client.post("/v1/auth/approle/login") .sendJsonObject(payload, ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }); }
/** * Logs in against the `userpass` backend. * * @param username the username * @param password the password * @param resultHandler the callback invoked with the result */ public void loginWithUserCredentials(String username, String password, Handler<AsyncResult<Auth>> resultHandler) { JsonObject payload = new JsonObject() .put("password", Objects.requireNonNull(password, "The password must not be null")); client.post("/v1/auth/userpass/login/" + Objects.requireNonNull(username, "The username must not be null")) .sendJsonObject(payload, ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }); }
/** * Creates a new token. * * @param tokenRequest the token request * @param resultHandler the callback invoked with the result. */ public void createToken(TokenRequest tokenRequest, Handler<AsyncResult<Auth>> resultHandler) { client.post("/v1/auth/token/create" + ((tokenRequest.getRole() == null) ? "" : "/" + tokenRequest.getRole())) .putHeader(TOKEN_HEADER, Objects.requireNonNull(getToken(), "The token must be set")) .sendJsonObject(tokenRequest.toPayload(), ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } manageAuthResult(resultHandler, ar.result()); }); }
/** * Write a secret to `path`. * * @param path the path * @param resultHandler the callback invoked with the result */ public void write(String path, JsonObject secrets, Handler<AsyncResult<Secret>> resultHandler) { Objects.requireNonNull(resultHandler); client.post("/v1/" + Objects.requireNonNull(path)) .putHeader(TOKEN_HEADER, Objects.requireNonNull(getToken(), "The token must be set")) .sendJsonObject(Objects.requireNonNull(secrets, "The secret must be set"), ar -> { if (ar.failed()) { resultHandler.handle(VaultException.toFailure("Unable to access the Vault", ar.cause())); return; } HttpResponse<Buffer> response = ar.result(); if (response.statusCode() == 200 || response.statusCode() == 204) { resultHandler.handle(Future.succeededFuture(response.bodyAsJson(Secret.class))); } else { resultHandler.handle(VaultException.toFailure(response.statusMessage(), response.statusCode(), response.bodyAsString())); } }); }
@Test public void testFileUploadWhenFileDoesNotExist() { HttpRequest<Buffer> builder = client.post("somepath"); MultipartForm form = MultipartForm.create() .textFileUpload("file", "nonexistentFilename", "nonexistentPathname", "text/plain"); builder.sendMultipartForm(form, onFailure(err -> { assertEquals(err.getClass(), HttpPostRequestEncoder.ErrorDataEncoderException.class); complete(); })); await(); }
@Test public void testFormUrlEncoded() throws Exception { server.requestHandler(req -> { req.setExpectMultipart(true); req.endHandler(v -> { assertEquals("param1_value", req.getFormAttribute("param1")); req.response().end(); }); }); startServer(); MultiMap form = MultiMap.caseInsensitiveMultiMap(); form.add("param1", "param1_value"); HttpRequest<Buffer> builder = client.post("/somepath"); builder.sendForm(form, onSuccess(resp -> complete())); await(); }
private void testSendBody(Object body, BiConsumer<String, Buffer> checker) throws Exception { waitFor(2); server.requestHandler(req -> req.bodyHandler(buff -> { checker.accept(req.getHeader("content-type"), buff); complete(); req.response().end(); })); startServer(); HttpRequest<Buffer> post = client.post(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, "/somepath"); if (body instanceof Buffer) { post.sendBuffer((Buffer) body, onSuccess(resp -> complete())); } else if (body instanceof JsonObject) { post.sendJsonObject((JsonObject) body, onSuccess(resp -> complete())); } else { post.sendJson(body, onSuccess(resp -> complete())); } await(); }
@Test public void testFormMultipart() throws Exception { server.requestHandler(req -> { req.setExpectMultipart(true); req.endHandler(v -> { assertEquals("param1_value", req.getFormAttribute("param1")); req.response().end(); }); }); startServer(); MultiMap form = MultiMap.caseInsensitiveMultiMap(); form.add("param1", "param1_value"); HttpRequest<Buffer> builder = client.post("/somepath"); builder.putHeader("content-type", "multipart/form-data"); builder.sendForm(form, onSuccess(resp -> complete())); await(); }
private void testFileUploadFormMultipart(int size) throws Exception { Buffer content = Buffer.buffer(TestUtils.randomAlphaString(size)); vertx.fileSystem().writeFileBlocking(testFile.getPath(), content); server.requestHandler(req -> { req.setExpectMultipart(true); req.uploadHandler(upload -> { Buffer fileBuffer = Buffer.buffer(); assertEquals("file", upload.name()); assertEquals("test.txt", upload.filename()); assertEquals("text/plain", upload.contentType()); upload.handler(fileBuffer::appendBuffer); upload.endHandler(v -> assertEquals(content, fileBuffer)); }); req.endHandler(v -> { assertEquals("vert.x", req.getFormAttribute("toolkit")); assertEquals("jvm", req.getFormAttribute("runtime")); req.response().end(); }); }); startServer(); MultipartForm form = MultipartForm.create() .attribute("toolkit", "vert.x") .attribute("runtime", "jvm") .textFileUpload("file", testFile.getName(), testFile.getPath(), "text/plain"); HttpRequest<Buffer> builder = client.post("somepath"); builder.sendMultipartForm(form, onSuccess(resp -> complete())); await(); }
@Test public void testInvalidRedirection() throws Exception { server.requestHandler(req -> { assertEquals(HttpMethod.POST, req.method()); assertEquals("/redirect", req.path()); req.response().setStatusCode(302).putHeader("Location", "http://www.google.com").end(); }); startServer(); HttpRequest<Buffer> builder = client .post("/redirect") .followRedirects(true); builder.send(onSuccess(resp -> { assertEquals(302, resp.statusCode()); assertEquals("http://www.google.com", resp.getHeader("Location")); assertNull(resp.body()); complete(); })); await(); }