Search in sources :

Example 1 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toFlowable().flatMap(resp -> {
        if (resp.statusCode() != 200) {
            throw new RuntimeException("Wrong status code " + resp.statusCode());
        }
        return Flowable.just(Buffer.buffer()).mergeWith(resp.toFlowable());
    }).reduce(Buffer::appendBuffer).map(buffer -> buffer.toString("UTF-8")).subscribe(data -> System.out.println("Server content " + data));
    // End request
    req.end();
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) Flowable(io.reactivex.Flowable) HttpMethod(io.vertx.core.http.HttpMethod) HttpClient(io.vertx.reactivex.core.http.HttpClient) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) Buffer(io.vertx.reactivex.core.buffer.Buffer) Buffer(io.vertx.reactivex.core.buffer.Buffer) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClient(io.vertx.reactivex.core.http.HttpClient)

Example 2 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project vertx-examples by vert-x3.

the class Client method start.

@Override
public void start() throws Exception {
    HttpClient client = vertx.createHttpClient();
    HttpClientRequest req = client.request(HttpMethod.GET, 8080, "localhost", "/");
    req.toFlowable().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();
}
Also used : HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpMethod(io.vertx.core.http.HttpMethod) HttpClient(io.vertx.reactivex.core.http.HttpClient) AbstractVerticle(io.vertx.reactivex.core.AbstractVerticle) Runner(io.vertx.example.util.Runner) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) HttpClient(io.vertx.reactivex.core.http.HttpClient)

Example 3 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project vertx-examples by vert-x3.

the class Client method start.

@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();
}
Also used : HttpClient(io.vertx.reactivex.core.http.HttpClient)

Example 4 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project knotx by Cognifide.

the class SampleApplicationTest method testGetServerError.

private void testGetServerError(TestContext context, String url) {
    HttpClient client = Vertx.newInstance(vertx.vertx()).createHttpClient();
    Async async = context.async();
    client.getNow(KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url, resp -> resp.bodyHandler(body -> {
        context.assertEquals(resp.statusCode(), HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
        client.close();
        async.complete();
    }));
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) RunWith(org.junit.runner.RunWith) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Logback(io.knotx.junit.rule.Logback) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Action1(rx.functions.Action1) Maps(com.google.common.collect.Maps) RuleChain(org.junit.rules.RuleChain) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) FileReader(io.knotx.junit.util.FileReader) HttpMethod(io.vertx.core.http.HttpMethod) Map(java.util.Map) Observable(io.reactivex.Observable) HttpClient(io.vertx.reactivex.core.http.HttpClient) Jsoup(org.jsoup.Jsoup) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) Async(io.vertx.ext.unit.Async) HttpClient(io.vertx.reactivex.core.http.HttpClient)

Example 5 with HttpClient

use of io.vertx.reactivex.core.http.HttpClient in project knotx by Cognifide.

the class KnotxServerRoutingTest method testPostRequest.

private void testPostRequest(String url, Action1<HttpClientResponse> expectedResponse) {
    HttpClient client = Vertx.newInstance(vertx.vertx()).createHttpClient();
    String testBody = "a=b";
    Observable<HttpClientResponse> request = request(client, HttpMethod.POST, KNOTX_SERVER_PORT, KNOTX_SERVER_ADDRESS, url, req -> {
        req.headers().set("content-length", String.valueOf(testBody.length()));
        req.headers().set("content-type", "application/x-www-form-urlencoded");
        req.write(testBody);
    });
    request.subscribe(expectedResponse::call);
}
Also used : HttpClient(io.vertx.reactivex.core.http.HttpClient) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse)

Aggregations

HttpClient (io.vertx.reactivex.core.http.HttpClient)12 HttpMethod (io.vertx.core.http.HttpMethod)8 HttpClientRequest (io.vertx.reactivex.core.http.HttpClientRequest)8 HttpClientResponse (io.vertx.reactivex.core.http.HttpClientResponse)7 Async (io.vertx.ext.unit.Async)6 KnotxConfiguration (io.knotx.junit.rule.KnotxConfiguration)5 Test (org.junit.Test)5 Logback (io.knotx.junit.rule.Logback)4 TestVertxDeployer (io.knotx.junit.rule.TestVertxDeployer)4 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)4 Observable (io.reactivex.Observable)4 Runner (io.vertx.example.util.Runner)4 TestContext (io.vertx.ext.unit.TestContext)4 RunTestOnContext (io.vertx.ext.unit.junit.RunTestOnContext)4 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)4 AbstractVerticle (io.vertx.reactivex.core.AbstractVerticle)4 Vertx (io.vertx.reactivex.core.Vertx)4 Rule (org.junit.Rule)4 RuleChain (org.junit.rules.RuleChain)4 RunWith (org.junit.runner.RunWith)4