/** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request <code>body</code> multimap encoded as form and the content type * set to <code>multipart/form-data</code>. You may use this method to send attributes and upload files. * @param body the body * @param handler */ public void sendMultipartForm(io.vertx.rxjava.ext.web.multipart.MultipartForm body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendMultipartForm(body.getDelegate(), new Handler<AsyncResult<io.vertx.ext.web.client.HttpResponse<T>>>() { public void handle(AsyncResult<io.vertx.ext.web.client.HttpResponse<T>> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance(ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
/** * Like {@link io.vertx.rxjava.ext.web.client.HttpRequest#send} but with an HTTP request <code>body</code> multimap encoded as form and the content type * set to <code>multipart/form-data</code>. You may use this method to send attributes and upload files. * @param body the body * @param handler */ public void sendMultipartForm(io.vertx.rxjava.ext.web.multipart.MultipartForm body, Handler<AsyncResult<io.vertx.rxjava.ext.web.client.HttpResponse<T>>> handler) { delegate.sendMultipartForm(body.getDelegate(), new Handler<AsyncResult<io.vertx.ext.web.client.HttpResponse<T>>>() { public void handle(AsyncResult<io.vertx.ext.web.client.HttpResponse<T>> ar) { if (ar.succeeded()) { handler.handle(io.vertx.core.Future.succeededFuture(io.vertx.rxjava.ext.web.client.HttpResponse.newInstance(ar.result(), __typeArg_0))); } else { handler.handle(io.vertx.core.Future.failedFuture(ar.cause())); } } }); }
@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(); }
req.sendJson("", handler); req.sendJsonObject(new JsonObject(), handler); req.sendMultipartForm(MultipartForm.create().attribute("a", "b"), handler);
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(); }