Search in sources :

Example 96 with HttpPut

use of org.apache.http.client.methods.HttpPut in project camel by apache.

the class CxfRsRouterTest method testPutConsumer.

@Test
public void testPutConsumer() throws Exception {
    HttpPut put = new HttpPut("http://localhost:" + getPort() + "/CxfRsRouterTest/route/customerservice/customers");
    StringEntity entity = new StringEntity(PUT_REQUEST, "ISO-8859-1");
    entity.setContentType("text/xml; charset=ISO-8859-1");
    put.setEntity(entity);
    CloseableHttpClient httpclient = HttpClientBuilder.create().build();
    try {
        HttpResponse response = httpclient.execute(put);
        assertEquals(200, response.getStatusLine().getStatusCode());
        assertEquals("", EntityUtils.toString(response.getEntity()));
    } finally {
        httpclient.close();
    }
}
Also used : StringEntity(org.apache.http.entity.StringEntity) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 97 with HttpPut

use of org.apache.http.client.methods.HttpPut in project openkit-android by OpenKit.

the class AsyncHttpClient method put.

/**
     * Perform a HTTP PUT request and track the Android Context which initiated the request.
     * And set one-time headers for the request
     * @param context the Android Context which initiated the request.
     * @param url the URL to send the request to.
     * @param headers set one-time headers for this request
     * @param entity a raw {@link HttpEntity} to send with the request, for example, use this to send string/json/xml payloads to a server by passing a {@link org.apache.http.entity.StringEntity}.
     * @param contentType the content type of the payload you are sending, for example application/json if sending a json payload.
     * @param responseHandler the response handler instance that should handle the response.
     */
public void put(Context context, String url, Header[] headers, HttpEntity entity, String contentType, AsyncHttpResponseHandler responseHandler) {
    HttpEntityEnclosingRequestBase request = addEntityToRequestBase(new HttpPut(url), entity);
    if (headers != null)
        request.setHeaders(headers);
    sendRequest(httpClient, httpContext, request, contentType, responseHandler, context);
}
Also used : HttpEntityEnclosingRequestBase(org.apache.http.client.methods.HttpEntityEnclosingRequestBase) HttpPut(org.apache.http.client.methods.HttpPut)

Example 98 with HttpPut

use of org.apache.http.client.methods.HttpPut in project mobile-android by photo.

the class ApiBase method createHttpRequest.

/**
     * Create a HttpUriRequest out of a ApiRequest object.
     * 
     * @param request the ApiRequest for which a HttpUriRequest should be
     *            created
     * @param baseUrl the base server url
     * @param listener Progress Listener with callback on progress
     * @return HttpUriRequest object which will do the request as described in
     *         ApiRequest
     * @throws UnsupportedEncodingException
     */
private HttpUriRequest createHttpRequest(ApiRequest request, String baseUrl, ProgressListener listener) throws UnsupportedEncodingException {
    HttpUriRequest httpRequest = null;
    switch(request.getMethod()) {
        case ApiRequest.GET:
            httpRequest = new HttpGet(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
        case ApiRequest.POST:
            httpRequest = new HttpPost(baseUrl + request.getPath());
            HttpPost httpPost = ((HttpPost) httpRequest);
            if (request.isMime()) {
                // TODO use the multipart when possible (currently server
                // handles it wrong)
                // HttpEntity entity = createMultipartEntity(request);
                // TODO remove this when doing correct multipart
                httpRequest = new HttpPost(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
                httpPost = ((HttpPost) httpRequest);
                HttpEntity entity = createFileOnlyMultipartEntity(request);
                if (listener != null) {
                    httpPost.setEntity(new HttpEntityWithProgress(entity, listener, httpPost));
                } else {
                    httpPost.setEntity(entity);
                }
            } else {
                httpPost.setEntity(new UrlEncodedFormEntity(request.getParameters(), HTTP.UTF_8));
            }
            break;
        case ApiRequest.PUT:
            httpRequest = new HttpPut(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
        case ApiRequest.DELETE:
            httpRequest = new HttpDelete(addParamsToUrl(baseUrl + request.getPath(), request.getParameters()));
            break;
    }
    for (NameValuePair pair : request.getHeaders()) {
        request.addHeader(pair.getName(), pair.getValue());
    }
    return httpRequest;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpPost(org.apache.http.client.methods.HttpPost) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) HttpPut(org.apache.http.client.methods.HttpPut)

Example 99 with HttpPut

use of org.apache.http.client.methods.HttpPut in project spark by perwendel.

the class SparkTestUtil method getHttpRequest.

private HttpUriRequest getHttpRequest(String requestMethod, String path, String body, boolean secureConnection, String acceptType, Map<String, String> reqHeaders) {
    try {
        String protocol = secureConnection ? "https" : "http";
        String uri = protocol + "://localhost:" + port + path;
        if (requestMethod.equals("GET")) {
            HttpGet httpGet = new HttpGet(uri);
            httpGet.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpGet);
            return httpGet;
        }
        if (requestMethod.equals("POST")) {
            HttpPost httpPost = new HttpPost(uri);
            httpPost.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPost);
            httpPost.setEntity(new StringEntity(body));
            return httpPost;
        }
        if (requestMethod.equals("PATCH")) {
            HttpPatch httpPatch = new HttpPatch(uri);
            httpPatch.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPatch);
            httpPatch.setEntity(new StringEntity(body));
            return httpPatch;
        }
        if (requestMethod.equals("DELETE")) {
            HttpDelete httpDelete = new HttpDelete(uri);
            addHeaders(reqHeaders, httpDelete);
            httpDelete.setHeader("Accept", acceptType);
            return httpDelete;
        }
        if (requestMethod.equals("PUT")) {
            HttpPut httpPut = new HttpPut(uri);
            httpPut.setHeader("Accept", acceptType);
            addHeaders(reqHeaders, httpPut);
            httpPut.setEntity(new StringEntity(body));
            return httpPut;
        }
        if (requestMethod.equals("HEAD")) {
            HttpHead httpHead = new HttpHead(uri);
            addHeaders(reqHeaders, httpHead);
            return httpHead;
        }
        if (requestMethod.equals("TRACE")) {
            HttpTrace httpTrace = new HttpTrace(uri);
            addHeaders(reqHeaders, httpTrace);
            return httpTrace;
        }
        if (requestMethod.equals("OPTIONS")) {
            HttpOptions httpOptions = new HttpOptions(uri);
            addHeaders(reqHeaders, httpOptions);
            return httpOptions;
        }
        if (requestMethod.equals("LOCK")) {
            HttpLock httpLock = new HttpLock(uri);
            addHeaders(reqHeaders, httpLock);
            return httpLock;
        }
        throw new IllegalArgumentException("Unknown method " + requestMethod);
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpGet(org.apache.http.client.methods.HttpGet) HttpOptions(org.apache.http.client.methods.HttpOptions) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpPatch(org.apache.http.client.methods.HttpPatch) HttpPut(org.apache.http.client.methods.HttpPut) HttpHead(org.apache.http.client.methods.HttpHead) StringEntity(org.apache.http.entity.StringEntity) HttpTrace(org.apache.http.client.methods.HttpTrace)

Example 100 with HttpPut

use of org.apache.http.client.methods.HttpPut in project robolectric by robolectric.

the class FakeHttpLayerTest method matches_shouldMatchPostBody.

@Test
public void matches_shouldMatchPostBody() throws Exception {
    final String expectedText = "some post body text";
    requestMatcherBuilder.postBody(new FakeHttpLayer.RequestMatcherBuilder.PostBodyMatcher() {

        @Override
        public boolean matches(HttpEntity actualPostBody) throws IOException {
            return EntityUtils.toString(actualPostBody).equals(expectedText);
        }
    });
    HttpPut match = new HttpPut("example.com");
    match.setEntity(new StringEntity(expectedText));
    HttpPost noMatch = new HttpPost("example.com");
    noMatch.setEntity(new StringEntity("some text that does not match"));
    assertThat(requestMatcherBuilder.matches(new HttpGet("example.com"))).isFalse();
    assertThat(requestMatcherBuilder.matches(noMatch)).isFalse();
    assertThat(requestMatcherBuilder.matches(match)).isTrue();
}
Also used : StringEntity(org.apache.http.entity.StringEntity) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Aggregations

HttpPut (org.apache.http.client.methods.HttpPut)153 StringEntity (org.apache.http.entity.StringEntity)89 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)50 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)40 HttpResponse (org.apache.http.HttpResponse)29 Test (org.junit.Test)29 JsonNode (com.fasterxml.jackson.databind.JsonNode)27 Deployment (org.activiti.engine.test.Deployment)27 HttpPost (org.apache.http.client.methods.HttpPost)19 HttpGet (org.apache.http.client.methods.HttpGet)17 IOException (java.io.IOException)16 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)16 HttpDelete (org.apache.http.client.methods.HttpDelete)14 HttpEntity (org.apache.http.HttpEntity)13 URI (java.net.URI)12 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)12 HttpHead (org.apache.http.client.methods.HttpHead)11 Execution (org.activiti.engine.runtime.Execution)10 ProcessInstance (org.activiti.engine.runtime.ProcessInstance)9 HttpEntityEnclosingRequestBase (org.apache.http.client.methods.HttpEntityEnclosingRequestBase)9