Example usage for io.vertx.core.http HttpClient post

List of usage examples for io.vertx.core.http HttpClient post

Introduction

In this page you can find the example usage for io.vertx.core.http HttpClient post.

Prototype

HttpClientRequest post(String requestURI);

Source Link

Document

Create an HTTP POST request to send to the server at the default host and port.

Usage

From source file:com.cyngn.vertx.bosun.BosunReporter.java

License:Apache License

/**
 * Send data to the bosun instance/* w ww. ja  v a2  s.c o m*/
 *
 * @param api the api on bosun to send to
 * @param data the json data to send
 * @param message the event bus message the request originated from
 */
private void sendData(String api, String data, Message message) {
    HttpClient client = getNextHost();

    Buffer buffer = Buffer.buffer(data.getBytes());

    client.post(api).exceptionHandler(error -> {
        sendError(message, "Got ex contacting bosun, " + error.getLocalizedMessage());
    }).handler(response -> {
        int statusCode = response.statusCode();
        // is it 2XX
        if (statusCode >= HttpResponseStatus.OK.code()
                && statusCode < HttpResponseStatus.MULTIPLE_CHOICES.code()) {
            message.reply(new JsonObject().put(RESULT_FIELD, BosunResponse.OK_MSG));
        } else {
            response.bodyHandler(responseData -> {
                sendError(message, "got non 200 response from bosun, error: " + responseData, statusCode);
            });
        }
    }).setTimeout(timeout).putHeader(HttpHeaders.CONTENT_LENGTH, buffer.length() + "")
            .putHeader(HttpHeaders.CONTENT_TYPE, MediaType.JSON_UTF_8.toString()).write(buffer).end();
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example43(HttpClient client) {

    HttpClientRequest request = client.post("some-uri");
    request.handler(response -> {/*w w w.ja  v a2  s. c o  m*/
        System.out.println("Received response with status code " + response.statusCode());
    });
}

From source file:io.gravitee.resource.oauth2.am.OAuth2AMResource.java

License:Apache License

@Override
public void introspect(String accessToken, Handler<OAuth2Response> responseHandler) {
    HttpClient httpClient = httpClients.computeIfAbsent(Vertx.currentContext(),
            context -> vertx.createHttpClient(httpClientOptions));

    logger.debug("Introspect access token by requesting {}", introspectionEndpointURI);

    HttpClientRequest request = httpClient.post(introspectionEndpointURI);

    request.headers().add(HttpHeaders.AUTHORIZATION, introspectionEndpointAuthorization);
    request.headers().add(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON);
    request.headers().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED);

    request.handler(response -> response.bodyHandler(buffer -> {
        logger.debug("AM Introspection endpoint returns a response with a {} status code",
                response.statusCode());/*from   w w w.j  a  v a 2s.c o  m*/
        if (response.statusCode() == HttpStatusCode.OK_200) {
            // Introspection Response for AM v2 always returns HTTP 200
            // with an "active" boolean indicator of whether or not the presented token is currently active.
            if (configuration().getVersion() == OAuth2ResourceConfiguration.Version.V2_X) {
                // retrieve active indicator
                JsonObject jsonObject = buffer.toJsonObject();
                boolean active = jsonObject.getBoolean(INTROSPECTION_ACTIVE_INDICATOR, false);
                responseHandler.handle(new OAuth2Response(active,
                        (active) ? buffer.toString() : "{\"error\": \"Invalid Access Token\"}"));
            } else {
                responseHandler.handle(new OAuth2Response(true, buffer.toString()));
            }
        } else {
            responseHandler.handle(new OAuth2Response(false, buffer.toString()));
        }
    }));

    request.exceptionHandler(event -> {
        logger.error("An error occurs while checking access_token", event);
        responseHandler.handle(new OAuth2Response(false, event.getMessage()));
    });

    request.end("token=" + accessToken);
}