Example usage for org.apache.http.entity StringEntity StringEntity

List of usage examples for org.apache.http.entity StringEntity StringEntity

Introduction

In this page you can find the example usage for org.apache.http.entity StringEntity StringEntity.

Prototype

public StringEntity(String str, Charset charset) 

Source Link

Usage

From source file:org.elasticsearch.client.RestHighLevelClientExtTests.java

public void testParseEntityCustomResponseSection() throws IOException {
    {/* ww w  .j  a va 2 s . co  m*/
        HttpEntity jsonEntity = new StringEntity("{\"custom1\":{ \"field\":\"value\"}}",
                ContentType.APPLICATION_JSON);
        BaseCustomResponseSection customSection = restHighLevelClient.parseEntity(jsonEntity,
                BaseCustomResponseSection::fromXContent);
        assertThat(customSection, instanceOf(CustomResponseSection1.class));
        CustomResponseSection1 customResponseSection1 = (CustomResponseSection1) customSection;
        assertEquals("value", customResponseSection1.value);
    }
    {
        HttpEntity jsonEntity = new StringEntity("{\"custom2\":{ \"array\": [\"item1\", \"item2\"]}}",
                ContentType.APPLICATION_JSON);
        BaseCustomResponseSection customSection = restHighLevelClient.parseEntity(jsonEntity,
                BaseCustomResponseSection::fromXContent);
        assertThat(customSection, instanceOf(CustomResponseSection2.class));
        CustomResponseSection2 customResponseSection2 = (CustomResponseSection2) customSection;
        assertArrayEquals(new String[] { "item1", "item2" }, customResponseSection2.values);
    }
}

From source file:ai.serotonin.haystack.validator.Source.java

/**
 * Read a remote database via the Project-Haystack protocol.
 * //from  w ww.  j  a va  2s .  c om
 * This method currently does not support authentication.
 * 
 * @param endpoint
 * @return the list of rows returned
 * @throws Exception
 */
public static List<HMap> remote(String endpoint) throws Exception {
    String filter = "id";
    //int limit = 10000;

    HMap map = new HMap().put("filter", filter); //.put("limit", limit);
    String entityStr = ZincWriter.gridToString(new HGrid(map));

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(endpoint + "read");
    // Set the auth as required.
    StringEntity entity = new StringEntity(entityStr, ContentType.TEXT_PLAIN);
    post.setEntity(entity);

    String responseStr = HttpUtils4.getTextContent(client, post, 1);
    HGrid response = new ZincReader(responseStr).readGrid();
    return response.getRows();
}

From source file:net.javacrumbs.restfire.httpcomponents.HttpComponentsRequestBuilder.java

public RequestBuilder withBody(String body) {
    setBody(new StringEntity(body, getContentType()));
    return this;
}

From source file:debop4k.http.gcm.GcmHttpPostTest.java

@Test
public void sendMulticastPushMessageToGCMServer() throws Exception {
    HttpPost post = new HttpPost(GcmSender.GCM_SERVER_URI);
    post.addHeader("Authorization", "key=" + SERVER_API_KEY);
    post.addHeader("Content-Type", "application/json");

    GcmMessage msg = newSampleGcmMessage();

    String text = jackson.toString(msg);
    log.debug("json text={}", text);

    post.setEntity(new StringEntity(text, Charsets.UTF_8));

    Promise<HttpResponse, Exception> future = AsyncHttpx.executeAsync(post);
    HttpResponse response = Asyncx.result(future);

    log.trace("Response={}", response.getStatusLine());
    assertThat(response.getStatusLine().getStatusCode()).isEqualTo(HttpStatus.SC_OK);
}

From source file:com.threatconnect.sdk.conn.HttpRequestExecutor.java

private void applyEntityAsJSON(HttpRequestBase httpBase, Object obj) throws JsonProcessingException {
    String jsonData = StringUtil.toJSON(obj);
    logger.trace("entity : " + jsonData);
    ((HttpEntityEnclosingRequestBase) httpBase)
            .setEntity(new StringEntity(jsonData, ContentType.APPLICATION_JSON));
}

From source file:org.elasticsearch.smoketest.MonitoringWithWatcherRestIT.java

@After
public void cleanExporters() throws Exception {
    String body = Strings.toString(jsonBuilder().startObject().startObject("transient")
            .nullField("xpack.monitoring.exporters.*").endObject().endObject());
    assertOK(adminClient().performRequest("PUT", "_cluster/settings", Collections.emptyMap(),
            new StringEntity(body, ContentType.APPLICATION_JSON)));

    assertOK(adminClient().performRequest("DELETE", ".watch*", Collections.emptyMap()));
}

From source file:com.wbtech.ums.common.NetworkUitlity.java

public static String Post(String url, String data) {

    CommonUtil.printLog("ums", url);

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url);
    try {//  ww  w. j a  va 2 s . co m
        StringEntity se = new StringEntity("content=" + data, HTTP.UTF_8);
        CommonUtil.printLog("postdata", "content=" + data);
        se.setContentType("application/x-www-form-urlencoded");
        httppost.setEntity(se);
        HttpResponse response = httpclient.execute(httppost);
        int status = response.getStatusLine().getStatusCode();
        CommonUtil.printLog("ums", status + "");
        String returnXML = EntityUtils.toString(response.getEntity());
        Log.d("returnString", URLDecoder.decode(returnXML));
        return URLDecoder.decode(returnXML);

    } catch (Exception e) {
        CommonUtil.printLog("ums", e.toString());
    }
    return null;
}

From source file:no.norrs.projects.andronary.service.utils.HttpUtil.java

public static HttpResponse POST(String uri, List<NameValuePair> data) throws IOException {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(uri);
    //httpPost.addHeader("Accept", "application/json");
    httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
    httpPost.addHeader("User-Agent", "Andronary/0.1");
    httpPost.addHeader("Connection", "close");
    StringEntity e = new StringEntity(data.get(0).getValue(), HTTP.UTF_8);
    //httpPost.setEntity(new UrlEncodedFormEntity(data));
    httpPost.setEntity(e);/*from w  w  w.ja  v  a  2s  .  co m*/
    HttpResponse response;

    return httpClient.execute(httpPost);

    //return response.getStatusLine().getStatusCode();

    /*HttpEntity entity = response.getEntity();
    return entity.getContent();*/

}

From source file:org.talend.dataprep.api.service.command.export.Export.java

/**
 * @param parameters the export parameters.
 * @return the request to perform./* ww  w.  j ava  2  s .  c om*/
 */
private HttpRequestBase onExecute(ExportParameters parameters) {
    try {
        final String parametersAsString = objectMapper.writerFor(ExportParameters.class)
                .writeValueAsString(parameters);
        final HttpPost post = new HttpPost(transformationServiceUrl + "/apply");
        post.setEntity(new StringEntity(parametersAsString, ContentType.APPLICATION_JSON));
        return post;
    } catch (Exception e) {
        throw new TDPException(APIErrorCodes.UNABLE_TO_EXPORT_CONTENT, e);
    }
}

From source file:pl.bcichecki.rms.client.android.services.clients.restful.impl.ProfileRestClient.java

public void updateProfile(User profile, AsyncHttpResponseHandler handler) throws UnsupportedEncodingException {
    String profileAsJson = GsonHolder.getGson().toJson(profile);
    HttpEntity profileAsHttpEntity = new StringEntity(profileAsJson, HttpConstants.CHARSET_UTF8);

    List<Header> headers = new ArrayList<Header>();
    RestUtils.decorareHeaderWithMD5(headers, profileAsJson);

    post(getContext(), getAbsoluteAddress(RestConstants.RESOURCE_PATH_PROFILE, RestConstants.RESOURCE_PATH_MY),
            getHeadersAsArray(headers), profileAsHttpEntity,
            HttpConstants.CONTENT_TYPE_APPLICATION_JSON_CHARSET_UTF8, handler);
}