Example usage for org.apache.http.client.fluent Request bodyString

List of usage examples for org.apache.http.client.fluent Request bodyString

Introduction

In this page you can find the example usage for org.apache.http.client.fluent Request bodyString.

Prototype

public Request bodyString(final String s, final ContentType contentType) 

Source Link

Usage

From source file:com.mywork.framework.util.RemoteHttpUtil.java

/**
 * ???json?post?// w w  w  .  j  a  v  a  2 s.co  m
 * 
 * @throws IOException
 * @throws
 * 
 */
public static String fetchJsonHttpResponse(String contentUrl, Map<String, String> headerMap,
        JsonObject bodyJson) throws IOException {
    Executor executor = Executor.newInstance(httpClient);
    Request request = Request.Post(contentUrl);
    if (headerMap != null && !headerMap.isEmpty()) {
        for (Map.Entry<String, String> m : headerMap.entrySet()) {
            request.addHeader(m.getKey(), m.getValue());
        }
    }
    if (bodyJson != null) {
        request.bodyString(bodyJson.toString(), ContentType.APPLICATION_JSON);
    }
    return executor.execute(request).returnContent().asString();
}

From source file:com.mywork.framework.util.RemoteHttpUtil.java

/**
 * ???json?post?/*from  ww w.  j  a  v  a  2  s.c o m*/
 *
 * @throws IOException
 *
 */
public static String fetchJsonHttpResponse(String contentUrl, Map<String, String> headerMap, String jsonBody)
        throws IOException {
    Executor executor = Executor.newInstance(httpClient);
    Request request = Request.Post(contentUrl);
    if (headerMap != null && !headerMap.isEmpty()) {
        for (Map.Entry<String, String> m : headerMap.entrySet()) {
            request.addHeader(m.getKey(), m.getValue());
        }
    }
    if (jsonBody != null) {
        request.bodyString(jsonBody, ContentType.APPLICATION_JSON);
    }
    long start = System.currentTimeMillis();
    String response = executor.execute(request).returnContent().asString();
    logger.info("url = " + contentUrl + " request spend time = " + (System.currentTimeMillis() - start));
    return response;
}

From source file:com.github.woki.payments.adyen.action.Endpoint.java

private static Request createPost(APService service, ClientConfig config, Object request) {
    Request retval = Post(config.getEndpointPort(service));
    // configure conn timeout
    retval.connectTimeout(config.getConnectionTimeout());
    // configure socket timeout
    retval.socketTimeout(config.getSocketTimeout());
    // add json/*  ww w . ja va2 s .  co  m*/
    retval.addHeader("Content-Type", "application/json");
    retval.addHeader("Accept", "application/json");
    for (Map.Entry<String, String> entry : config.getExtraParameters().entrySet()) {
        retval.addHeader(entry.getKey(), entry.getValue());
    }
    // add content
    String bodyString;
    try {
        bodyString = MAPPER.writeValueAsString(encrypt(config, request));
    } catch (Exception e) {
        throw new RuntimeException("CSE/JSON serialization error", e);
    }
    retval.bodyString(bodyString, ContentType.APPLICATION_JSON);
    if (config.hasProxy()) {
        retval.viaProxy(config.getProxyHost());
    }
    return retval;
}

From source file:nebula.plugin.metrics.dispatcher.SplunkMetricsDispatcher.java

@Override
protected void postPayload(String requestBody) {
    try {//from   ww w.  j av a  2 s. c  o m

        Request postReq = Request.Post(extension.getSplunkUri());
        postReq.bodyString(requestBody, ContentType.APPLICATION_JSON);
        addHeaders(postReq);
        StatusLine status = postReq.execute().returnResponse().getStatusLine();

        if (SC_OK != status.getStatusCode()) {
            error = String.format("%s (status code: %s)", status.getReasonPhrase(), status.getStatusCode());
        }
    } catch (IOException e) {
        error = e.getMessage();
    }
}

From source file:io.gravitee.gateway.standalone.OverrideRequestContentGatewayTest.java

@Test
public void call_override_request_content() throws Exception {
    org.apache.http.client.fluent.Request request = org.apache.http.client.fluent.Request
            .Post("http://localhost:8082/echo/helloworld");
    request.bodyString("Request content overriden by policy", ContentType.TEXT_PLAIN);

    org.apache.http.client.fluent.Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(OverrideRequestContentPolicy.STREAM_POLICY_CONTENT, responseContent);
}

From source file:io.gravitee.gateway.standalone.OverrideResponseContentGatewayTest.java

@Test
public void call_override_response_content() throws Exception {
    org.apache.http.client.fluent.Request request = org.apache.http.client.fluent.Request
            .Post("http://localhost:8082/echo/helloworld");
    request.bodyString("This content should normally be returned by echo backend", ContentType.TEXT_PLAIN);

    org.apache.http.client.fluent.Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    assertEquals(OverrideResponseContentPolicy.STREAM_POLICY_CONTENT, responseContent);
}

From source file:io.gravitee.gateway.standalone.TransformResponseContentUsingBuilderGatewayTest.java

@Test
public void call_override_request_content() throws Exception {
    org.apache.http.client.fluent.Request request = org.apache.http.client.fluent.Request
            .Post("http://localhost:8082/echo/helloworld");
    request.bodyString(BODY_CONTENT + " {#request.id}", ContentType.TEXT_PLAIN);

    org.apache.http.client.fluent.Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());

    assertEquals("A new content", responseContent);
}

From source file:nebula.plugin.metrics.dispatcher.RestMetricsDispatcher.java

protected void postPayload(String payload) {
    checkNotNull(payload);/*  w ww. j a  v  a 2 s  . com*/

    try {
        Request postReq = Request.Post(extension.getRestUri());
        postReq.bodyString(payload, ContentType.APPLICATION_JSON);
        addHeaders(postReq);
        postReq.execute();
    } catch (IOException e) {
        throw new RuntimeException("Unable to POST to " + extension.getRestUri(), e);
    }
}

From source file:io.gravitee.gateway.standalone.TransformRequestContentGatewayTest.java

@Test
public void call_override_request_content() throws Exception {
    org.apache.http.client.fluent.Request request = org.apache.http.client.fluent.Request
            .Post("http://localhost:8082/echo/helloworld");
    request.bodyString(BODY_CONTENT + " {#request.id}", ContentType.TEXT_PLAIN);

    org.apache.http.client.fluent.Response response = request.execute();
    HttpResponse returnResponse = response.returnResponse();

    assertEquals(HttpStatus.SC_OK, returnResponse.getStatusLine().getStatusCode());

    String responseContent = StringUtils.copy(returnResponse.getEntity().getContent());
    String[] parts = responseContent.split(":");

    assertTrue(responseContent.startsWith(BODY_CONTENT));
    assertTrue(UUID.fromString(parts[1].substring(1)) != null);
}

From source file:edu.jhuapl.dorset.http.apache.ApacheHttpClient.java

private Response put(HttpRequest request) throws IOException {
    Request apacheRequest = Request.Put(request.getUrl());
    if (request.getBody() != null) {
        ContentType ct = ContentType.create(request.getContentType().getMimeType(),
                request.getContentType().getCharset());
        apacheRequest.bodyString(request.getBody(), ct);
    } else if (request.getBodyForm() != null) {
        apacheRequest.bodyForm(buildFormBody(request.getBodyForm()));
    }//from   w  w w.j  a v  a 2  s . c  o  m
    prepareRequest(apacheRequest);
    return apacheRequest.execute();
}