use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.
the class SingleAddressRedirectClient method main.
public static void main(String... args) throws Exception {
try (HttpClient client = HttpClients.forSingleAddress("localhost", NON_SECURE_SERVER_PORT).appendClientFilter(new RedirectingHttpRequesterFilter(new RedirectConfigBuilder().allowedMethods(GET, // by default, POST requests don't follow redirects:
POST).build())).build()) {
System.out.println("- Simple GET request:");
client.request(client.get("/relative")).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerAscii()));
System.out.println();
}).toFuture().get();
System.out.println("- POST request with headers and payload body:");
client.request(client.post("/relative").addHeader(CUSTOM_HEADER, "value").payloadBody(client.executionContext().bufferAllocator().fromAscii("some_content"))).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerAscii()));
System.out.println();
}).toFuture().get();
}
}
use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.
the class RetryClient method main.
public static void main(String[] args) throws Exception {
try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080).appendClientFilter(new RetryingHttpRequesterFilter.Builder().responseMapper(httpResponseMetaData -> SERVER_ERROR_5XX.contains(httpResponseMetaData.status()) ? // Response status is 500-599, we request a retry
new RetryingHttpRequesterFilter.HttpResponseException("Retry 5XX", httpResponseMetaData) : // Not a 5XX response, we do not know whether retry is required
null).retryResponses((meta, error) -> RetryingHttpRequesterFilter.BackOffPolicy.ofImmediate(2)).build()).build()) {
client.request(client.get("/sayHello")).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(textSerializerUtf8()));
}).toFuture().get();
}
}
use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.
the class PojoUrlClient method main.
public static void main(String[] args) throws Exception {
try (HttpClient client = HttpClients.forMultiAddressUrl().build()) {
client.request(client.post("http://localhost:8080/pojos").payloadBody(new CreatePojoRequest("value"), REQ_SERIALIZER)).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(RESP_SERIALIZER));
}).toFuture().get();
}
}
use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.
the class ProtobufClient method main.
public static void main(String[] args) throws Exception {
try (HttpClient client = HttpClients.forSingleAddress("localhost", 8080).build()) {
client.request(client.post("/protobuf").payloadBody(RequestMessage.newBuilder().setMessage("value").build(), REQ_SERIALIZER)).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(RESP_SERIALIZER));
}).toFuture().get();
}
}
use of io.servicetalk.http.api.HttpClient in project servicetalk by apple.
the class ProtobufUrlClient method main.
public static void main(String[] args) throws Exception {
try (HttpClient client = HttpClients.forMultiAddressUrl().build()) {
client.request(client.post("http://localhost:8080/protobuf").payloadBody(ExampleProtos.RequestMessage.newBuilder().setMessage("hello").build(), REQ_SERIALIZER)).whenOnSuccess(resp -> {
System.out.println(resp.toString((name, value) -> value));
System.out.println(resp.payloadBody(RESP_SERIALIZER));
}).toFuture().get();
}
}
Aggregations