Java Code Examples for io.vertx.core.http.HttpClient#post()

The following examples show how to use io.vertx.core.http.HttpClient#post() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MetricsServiceImplTest.java    From vertx-micrometer-metrics with Apache License 2.0 6 votes vote down vote up
private void runClientRequests(TestContext ctx, HttpClient httpClient, int count, String path) {
  Async async = ctx.async(count);
  for (int i = 0; i < count; i++) {
    httpClient.post(9195, "127.0.0.1", path, Buffer.buffer(CLIENT_REQUEST), ar -> {
      if (ar.succeeded()) {
        HttpClientResponse response = ar.result();
        async.countDown();
        if (response.statusCode() != 200) {
          ctx.fail(response.statusMessage());
        }
      } else {
        async.countDown();
        ctx.fail(ar.cause());
      }
    });
  }
  async.await();
}
 
Example 2
Source File: PauseResumeTest.java    From okapi with Apache License 2.0 6 votes vote down vote up
private void myStreamHandle2(RoutingContext ctx) {
  ctx.request().pause();
  HttpClient cli = vertx.createHttpClient();
  HttpClientRequest req = cli.post(PORT, "localhost", "/test1", res -> {
    if (ctx.request().isEnded()) {
      ctx.response().end("OK2"); // Vert.x 3.6 series
    } else {
      ctx.request().endHandler(x -> {
        ctx.response().end("OK2");
      });
    }
    ctx.request().resume();
  });
  req.exceptionHandler(x -> {
    ctx.response().setStatusCode(500);
    ctx.response().end(x.getLocalizedMessage());
  });
  req.end();
}
 
Example 3
Source File: MultipartRequestTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleUploadMutation() {
  final HttpClient client = vertx.createHttpClient(getHttpClientOptions());

  final Buffer bodyBuffer = vertx.fileSystem().readFileBlocking("singleUpload.txt");

  client.post(new RequestOptions()
    .setURI("/graphql")
    .setTimeout(10000)
    .addHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryBpwmk50wSJmsTPAH")
    .addHeader("accept", "application/json"),
    bodyBuffer,
    response -> response.result().bodyHandler(buffer -> {
    final JsonObject json = ((JsonObject) buffer.toJson()).getJsonObject("data").getJsonObject("singleUpload");
    assertEquals("a.txt", json.getString("id"));
    complete();
  }));

  await();
}
 
Example 4
Source File: MultipartRequestTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultipleUploadMutation() {
  final HttpClient client = vertx.createHttpClient(getHttpClientOptions());

  final Buffer bodyBuffer = vertx.fileSystem().readFileBlocking("multipleUpload.txt");
  client.post(new RequestOptions()
    .setURI("/graphql")
    .addHeader("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundaryhvb6BzAACEqQKt0Z")
    .addHeader("accept", "application/json")
    .setTimeout(10000), bodyBuffer, response -> response.result().bodyHandler(buffer -> {
    final JsonObject json = ((JsonObject) buffer.toJson()).getJsonObject("data").getJsonObject("multipleUpload");
    assertEquals("b.txt c.txt", json.getString("id"));
    complete();
  }));

  await();
}
 
Example 5
Source File: VertxHttpClientServerMetricsTest.java    From vertx-micrometer-metrics with Apache License 2.0 5 votes vote down vote up
private void httpRequest(HttpClient httpClient, TestContext ctx) {
  Async async = ctx.async(HTTP_SENT_COUNT);
  for (int i = 0; i < HTTP_SENT_COUNT; i++) {
    httpClient.post(9195, "127.0.0.1", "/resource", Buffer.buffer(CLIENT_REQUEST), ctx.asyncAssertSuccess(response -> {
      async.countDown();
      if (response.statusCode() != 200) {
        ctx.fail(response.statusMessage());
      }
    }));
  }
  async.await();
}
 
Example 6
Source File: DemoRamlRestTest.java    From raml-module-builder with Apache License 2.0 4 votes vote down vote up
private void testStream(TestContext context, boolean chunk) {
  int chunkSize = 1024;
  int numberChunks = 50;
  Async async = context.async();
  HttpClient httpClient = vertx.createHttpClient();
  HttpClientRequest req = httpClient.post(port, "localhost", "/rmbtests/testStream", res -> {
    Buffer resBuf = Buffer.buffer();
    res.handler(resBuf::appendBuffer);
    res.endHandler(x -> {
      context.assertEquals(200, res.statusCode());
      JsonObject jo = new JsonObject(resBuf);
      context.assertTrue(jo.getBoolean("complete"));
      async.complete();
    });
    res.exceptionHandler(x -> {
      if (!async.isCompleted()) {
        context.assertTrue(false, "exceptionHandler res: " + x.getLocalizedMessage());
        async.complete();
      }
    });
  });
  req.exceptionHandler(x -> {
    if (!async.isCompleted()) {
      context.assertTrue(false, "exceptionHandler req: " + x.getLocalizedMessage());
      async.complete();
    }
  });
  if (chunk) {
    req.setChunked(true);
  } else {
    req.putHeader("Content-Length", Integer.toString(chunkSize * numberChunks));
  }
  req.putHeader("Accept", "application/json,text/plain");
  req.putHeader("Content-type", "application/octet-stream");
  req.putHeader("x-okapi-tenant", TENANT);
  Buffer buf = Buffer.buffer(chunkSize);
  for (int i = 0; i < chunkSize; i++) {
    buf.appendString("X");
  }
  for (int i = 0; i < numberChunks; i++) {
    req.write(buf);
  }
  req.end();
}