Package org.apache.http.entity

Examples of org.apache.http.entity.StringEntity


    */
   @Test
   public void testOutboundRewritingSpaceCharacter() throws Exception
   {
      String url = "/encoding.html?param=foo+bar";
      String rewritten = post("/outbound", new StringEntity(url));

      assertThat(rewritten, is("/encoding/foo%20bar"));
   }
View Full Code Here


    */
   @Test
   public void testOutboundRewritingAmpersandCharacter() throws Exception
   {
      String url = "/encoding.html?param=foo%26bar";
      String rewritten = post("/outbound", new StringEntity(url));

      assertThat(rewritten, is("/encoding/foo&bar"));
   }
View Full Code Here

   * @return Server's response body.
   */
  public String makePostRequest(String url, String jsonPayload) {
    try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
      HttpPost post = new HttpPost(url);
      post.setEntity(new StringEntity(jsonPayload, "utf-8"));
      post.addHeader("Content-Type", "application/json; charset=utf-8");

      return httpClient.execute(post, new BasicResponseHandler());
    } catch (IOException e) {
      throw new LogCommunicationException("Error making POST request to " + url, e);
View Full Code Here

    private HttpEntity createEntity(JobScript script) {
        assert script != null;
        String json = GSON_BUILDER.create().toJson(script);
        LOG.trace("request: {}", json);
        return new StringEntity(json, CONTENT_TYPE);
    }
View Full Code Here

        }

        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
            response.setStatusCode(200);
            response.setEntity(new StringEntity(
                    new Gson().toJson(responseElement).toString(), HttpJobClient.CONTENT_TYPE));
            if (request instanceof HttpEntityEnclosingRequest) {
                HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                String content = EntityUtils.toString(entity, "UTF-8");
                JsonElement element = parse(content);
View Full Code Here

            response.setStatusCode(status);
            if (code != null) {
                JsonObject object = new JsonObject();
                object.addProperty("error", code);
                object.addProperty("message", code);
                response.setEntity(new StringEntity(new Gson().toJson(object).toString(), HttpJobClient.CONTENT_TYPE));
            }
            if (request instanceof HttpEntityEnclosingRequest) {
                HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                String content = EntityUtils.toString(entity, "UTF-8");
                JsonElement element = parse(content);
View Full Code Here

 
 
  /*
   * Edit the value de gender and birthdate the one record of person
   */
  StringEntity inputJsonEdit = new StringEntity(
          "{\"gender\":\"F\",\"birthdate\":\"1988-08-08T00:00:00.000-0300\"}"
          );
  inputJsonEdit.setContentType("application/json");
   System.out.println("EditPerson = " +
           ApiAuthRest.getRequestPost(
               "person/d0c9bd52-99dd-11e1-acb0-00188b78ce12",
               inputJsonEdit));
   System.out.println("########################");
  
   /*
    * Edit the value de gender and birthdate the one record of person
    */
   StringEntity inputAddPerson = new StringEntity(
      "{\"names\":[{\"givenName\": \"John\",\"familyName\":\"Soto\"}],\"gender\":\"F\",\"age\":40}"
      );
  
   inputAddPerson.setContentType("application/json");
   System.out.println("AddPerson = " +
           ApiAuthRest.getRequestPost(
               "person",
               inputAddPerson));
   System.out.println("########################");
View Full Code Here

                    httpMethod = new HttpDelete(url);
                    break;

                case POST:
                    httpMethod = new HttpPost(url);
                    ((HttpPost) httpMethod).setEntity(new StringEntity(this.requestBody));
                    break;
                case PUT:
                    httpMethod = new HttpPut(url);
                    ((HttpPut) httpMethod).setEntity(new StringEntity(this.requestBody));
                    break;
                default:
                    throw new IllegalStateException("Can't execute this method : " + method);
            }
View Full Code Here

   
    private Long doAuthenticatedPost(String url, String body) throws Exception {
        HttpPost request = new HttpPost(url + "?oauth_token=" + oauthToken);
        HttpResponse response = null;
        try {
            HttpEntity entity = new StringEntity(body, HTTP.UTF_8);
            // set the content type to json
            request.setHeader("Content-Type", "application/json; charset=utf-8");
            request.setEntity(entity);
            // sign the request
            // send the request
            response = httpClient.execute(request);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 401) {
                throw new RuntimeException("unable to execute POST - url: "
                    + url
                    + " body: "
                    + body);
            }
           
            if (statusCode == 201) {
                Header loc = response.getFirstHeader("Location");
                if (loc != null) {
                    String locS = loc.getValue();
                    if (!StringUtils.isBlank(locS) && locS.matches(".*/[0-9]+$")) {
                        try {
                            return NumberUtils.createLong(
                                    locS.substring(locS.lastIndexOf("/") + 1));
                        } catch (NumberFormatException e) {
                            return null;
                        }
                    }
                }
            }
           
            return null;
        } finally {
            try {
                if (response != null) {
                    HttpEntity entity = response.getEntity();
                    if (entity != null) {
                        entity.consumeContent();
                    }
                }
                //EntityUtils.consume(response.getEntity());
            } catch (IOException e) {
                // ignore
View Full Code Here

        public void handle(
                final HttpRequest request,
                final HttpResponse response,
                final HttpContext context) throws HttpException, IOException {
            response.setStatusCode(HttpStatus.SC_OK);
            StringEntity entity = new StringEntity("Whatever");
            response.setEntity(entity);
        }
View Full Code Here

TOP

Related Classes of org.apache.http.entity.StringEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.