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

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

Introduction

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

Prototype

public static Request Post(final String uri) 

Source Link

Usage

From source file:org.mule.module.http.functional.listener.HttpListenerPathRoutingTestCase.java

@Test
public void callPath() throws Exception {
    final String url = String.format("http://localhost:%s/%s", listenPort.getNumber(), testPath);
    final Response response = Request.Post(url).body(new StringEntity(testPath)).connectTimeout(1000).execute();
    assertThat(response.returnContent().asString(), is(testPath));
}

From source file:rxweb.RxJavaServerTests.java

public void echo() throws IOException {
    server.post("/test", (request, response) -> response.content(request.getContent()));
    String content = Request.Post("http://localhost:8080/test")
            .bodyString("This is a test!", ContentType.TEXT_PLAIN).execute().returnContent().asString();
    Assert.assertEquals("This is a test!", content);
}

From source file:org.mule.module.http.functional.listener.HttpListenerEncodingTestCase.java

@Test
public void testEncoding() throws Exception {
    final String url = String.format("http://localhost:%s/test", port.getNumber());
    Request request = Request.Post(url).bodyString(testMessage,
            ContentType.create("text/plain", Charset.forName(encoding)));
    request.execute();//www  .  ja v  a2 s  .  c  o  m
    MuleMessage result = muleContext.getClient().request("vm://out", 2000);
    assertThat(result.getPayloadAsString(), is(testMessage));
}

From source file:org.mule.module.http.functional.listener.HttpListenerMethodRoutingTestCase.java

@Test
public void callWithMethod() throws Exception {
    final String url = String.format("http://localhost:%s/%s", listenPort.getNumber(), path.getValue());
    Request request = null;//from   w w w .  j  av  a2 s.  c o  m
    switch (method) {
    case "GET":
        request = Request.Get(url);
        break;
    case "POST":
        request = Request.Post(url);
        break;
    case "OPTIONS":
        request = Request.Options(url);
        break;
    case "DELETE":
        request = Request.Delete(url);
        break;
    case "PUT":
        request = Request.Put(url);
        break;
    }
    final Response response = request.connectTimeout(1000).execute();
    final HttpResponse httpResponse = response.returnResponse();
    assertThat(httpResponse.getStatusLine().getStatusCode(), is(200));
    assertThat(IOUtils.toString(httpResponse.getEntity().getContent()), is(expectedContent));
}

From source file:com.m3958.vertxio.assetfeed.integration.java.FileUploadTest.java

@Test
public void testPostRename() throws ClientProtocolException, IOException, URISyntaxException {

    File f = new File("README.md");

    InputStream is = Files.asByteSource(f).openBufferedStream();

    Assert.assertTrue(f.exists());/*  w  w  w.  j a v a  2 s  .  c  o  m*/

    String c = Request.Post(new URIBuilder().setScheme("http").setHost("localhost").setPort(8080).build())
            .body(MultipartEntityBuilder.create()
                    .addBinaryBody("afile", is, ContentType.MULTIPART_FORM_DATA, f.getName())
                    .addTextBody("fn", "abcfn.md").build())
            .execute().returnContent().asString();

    String url = c.trim();
    Assert.assertTrue(url.endsWith("abcfn.md"));

    testComplete();
}

From source file:com.mdmserver.mdb.ControllerQueue.java

private void sendGCMMessge(String cloudId, String jsonData) {
    try {/*from   w w  w  . j  a v  a2 s  . c  o  m*/
        //                Request.Post("http://android.googleapis.com/gcm/send").setHeader("Authorization", "key="+Config.gcmServerId)
        //                        .bodyString("{\"registration_ids\":[\""+cloudId+"\"],\"data\": {\"1\":\"2\",\"3\":\"4\"}}", ContentType.APPLICATION_JSON)
        //                        .execute().returnContent();
        Logger.getLogger("Avin").log(Level.INFO,
                "{\"registration_ids\":[\"" + cloudId + "\"],\"data\":" + jsonData + "}");
        Request.Post("http://android.googleapis.com/gcm/send")
                .setHeader("Authorization", "key=" + Config.gcmServerId)
                .bodyString("{\"registration_ids\":[\"" + cloudId + "\"],\"data\":" + jsonData + "}",
                        ContentType.APPLICATION_JSON)
                .execute().returnContent();
        System.out.println("Sending----------------------");
    } catch (IOException ex) {
        Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.github.piotrkot.auth.SimpleAuthenticator.java

@Override
public Optional<User> authenticate(final BasicCredentials cred) {
    Optional<User> option = Optional.absent();
    try {//from w w  w. j  av a  2s . c  om
        final User user = new User(cred.getUsername(), cred.getPassword());
        Request.Post(String.format("%s/auth", this.conf.getCompiler()))
                .setHeader("Authorization", user.toBase64()).execute().returnContent().asString();
        option = Optional.of(user);
    } catch (final IOException ex) {
        log.error("Authentication failure", ex);
    }
    return option;
}

From source file:org.mule.module.http.functional.listener.HttpListenerContentTypeTestCase.java

@Test
public void rejectsInvalidContentTypeWithBody() throws Exception {
    Request request = Request.Post(getUrl()).body(new StringEntity(TEST_MESSAGE, "application", null));
    testRejectContentType(request, "Could not parse");
}

From source file:com.qwazr.search.index.IndexSingleClient.java

@Override
public SchemaSettingsDefinition createUpdateSchema(String schema_name) {
    UBuilder uriBuilder = new UBuilder("/indexes/", schema_name);
    Request request = Request.Post(uriBuilder.build());
    return commonServiceRequest(request, null, null, SchemaSettingsDefinition.class, 200);
}

From source file:org.restheart.test.integration.PostCollectionIT.java

@Test
public void testPostCollection() throws Exception {
    try {//from  w  w w.j  a  v a2  s .  c om
        Response resp;

        // *** PUT tmpdb
        resp = adminExecutor.execute(Request.Put(dbTmpUri).bodyString("{a:1}", halCT)
                .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE));
        check("check put db", resp, HttpStatus.SC_CREATED);

        // *** PUT tmpcoll
        resp = adminExecutor.execute(Request.Put(collectionTmpUri).bodyString("{a:1}", halCT)
                .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE));
        check("check put coll1", resp, HttpStatus.SC_CREATED);

        resp = adminExecutor.execute(Request.Post(collectionTmpUri).bodyString("{a:1}", halCT)
                .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE));
        check("check post coll1", resp, HttpStatus.SC_CREATED);

        // *** POST tmpcoll
        resp = adminExecutor.execute(Request.Post(collectionTmpUri).bodyString("{a:1}", halCT)
                .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE));
        HttpResponse httpResp = check("check post coll1 again", resp, HttpStatus.SC_CREATED);

        Header[] headers = httpResp.getHeaders(Headers.LOCATION_STRING);

        assertNotNull("check loocation header", headers);
        assertTrue("check loocation header", headers.length > 0);

        Header locationH = headers[0];
        String location = locationH.getValue();

        URI createdDocUri = URI.create(location);

        resp = adminExecutor.execute(Request.Get(createdDocUri).addHeader(Headers.CONTENT_TYPE_STRING,
                Representation.HAL_JSON_MEDIA_TYPE));

        JsonObject content = JsonObject.readFrom(resp.returnContent().asString());
        assertNotNull("check created doc content", content.get("_id"));
        assertNotNull("check created doc content", content.get("_etag"));
        assertNotNull("check created doc content", content.get("a"));
        assertTrue("check created doc content", content.get("a").asInt() == 1);

        String _id = content.get("_id").asObject().get("$oid").asString();
        String _etag = content.get("_etag").asObject().get("$oid").asString();

        // try to post with _id without etag
        resp = adminExecutor.execute(
                Request.Post(collectionTmpUri).bodyString("{_id:{\"$oid\":\"" + _id + "\"}, a:1}", halCT)
                        .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE));
        check("check post created doc without etag", resp, HttpStatus.SC_CONFLICT);

        // try to post with wrong etag
        resp = adminExecutor.execute(
                Request.Post(collectionTmpUri).bodyString("{_id:{\"$oid\":\"" + _id + "\"}, a:1}", halCT)
                        .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE)
                        .addHeader(Headers.IF_MATCH_STRING, "pippoetag"));
        check("check put created doc with wrong etag", resp, HttpStatus.SC_PRECONDITION_FAILED);

        // try to post with correct etag
        resp = adminExecutor.execute(
                Request.Post(collectionTmpUri).bodyString("{_id:{\"$oid\":\"" + _id + "\"}, a:1}", halCT)
                        .addHeader(Headers.CONTENT_TYPE_STRING, Representation.HAL_JSON_MEDIA_TYPE)
                        .addHeader(Headers.IF_MATCH_STRING, _etag));
        check("check post created doc with correct etag", resp, HttpStatus.SC_OK);
    } finally {
        mongoClient.dropDatabase(dbTmpName);
    }
}