Refine search
URL url = new URL("http://www.example.com/resource"); HttpClient client = new DefaultHttpClient(); HttpPut put= new HttpPut(url); List<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("key1", "value1")); pairs.add(new BasicNameValuePair("key2", "value2")); put.setEntity(new UrlEncodedFormEntity(pairs)); HttpResponse response = client.execute(put);
public HttpResponse doPut(String query, String username, String password, String body) { HttpPut httpPut = new HttpPut(context + query); httpPut.addHeader("content-type", "application/json"); httpPut.setEntity(new StringEntity(body, StandardCharsets.UTF_8)); return doRequest(httpPut, username, password); }
public void append(String content) throws IOException { HttpPut putMethod = new HttpPut(consoleUri); try { LOGGER.debug("Appending console to URL -> {}", consoleUri); StringEntity entity = new StringEntity(content, charset); putMethod.setEntity(entity); HttpService.setSizeHeader(putMethod, entity.getContentLength()); CloseableHttpResponse response = httpService.execute(putMethod); LOGGER.debug("Got {}", response.getStatusLine().getStatusCode()); } finally { putMethod.releaseConnection(); } } }
public void wipeCache(String entity, String event, String cacheKey) throws IOException { HttpPut request; String url; if (cacheKey.contains("/")) { url = baseUrl + "/cache/" + entity + "/" + event; request = new HttpPut(url); request.setEntity(new StringEntity(cacheKey, ContentType.create(APPLICATION_JSON, UTF_8))); } else { url = baseUrl + "/cache/" + entity + "/" + cacheKey + "/" + event; request = new HttpPut(url); } HttpResponse response = null; try { response = client.execute(request); if (response.getStatusLine().getStatusCode() != 200) { String msg = EntityUtils.toString(response.getEntity()); throw new IOException(INVALID_RESPONSE + response.getStatusLine().getStatusCode() + " with cache wipe url " + url + "\n" + msg); } } finally { cleanup(request, response); } }
/** * Send a PUT request * @param cluster the cluster definition * @param path the path or URI * @param headers the HTTP headers to include, <tt>Content-Type</tt> must be * supplied * @param content the content bytes * @return a Response object with response detail * @throws IOException */ public Response put(Cluster cluster, String path, Header[] headers, byte[] content) throws IOException { HttpPut method = new HttpPut(path); try { method.setEntity(new InputStreamEntity(new ByteArrayInputStream(content), content.length)); HttpResponse resp = execute(cluster, method, headers, path); headers = resp.getAllHeaders(); content = getResponseBody(resp); return new Response(resp.getStatusLine().getStatusCode(), headers, content); } finally { method.releaseConnection(); } }
public HttpResponse executePutRequest(final String request, String body, Header... header) throws Exception { HttpPut uriRequest = new HttpPut(getHttpServerUri() + "/" + request); if (body != null && !body.isEmpty()) { uriRequest.setEntity(new StringEntity(body)); } return executeRequest(uriRequest, header); }
@Test public void parseParams_shouldParsePutEntitiesIntoParams() throws Exception { HttpPut put = new HttpPut("example.com"); StringEntity entity = new StringEntity("param1=foobar"); entity.setContentType("application/x-www-form-urlencoded"); put.setEntity(entity); Map<String,String> params = ParamsParser.parseParams(put); assertThat(params.get("param1")).isEqualTo("foobar"); }
@Test public void matches_shouldMatchPostBody() throws Exception { final String expectedText = "some post body text"; requestMatcherBuilder.postBody( actualPostBody -> 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(); } }
@Test public void test() throws Exception { try (CloseableHttpClient client = HttpClientBuilder.create().build()) { HttpPut put = new HttpPut( String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); put.setEntity(EntityBuilder.create().setText(VALUE) .setContentType(ContentType.create("text-plain", StandardCharsets.UTF_8)).build()); try (CloseableHttpResponse resp = client.execute(put)) { assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); } HttpGet get = new HttpGet( String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), ROW, FAMILY, QUALIFIER)); try (CloseableHttpResponse resp = client.execute(get)) { assertEquals(HttpStatus.SC_OK, resp.getStatusLine().getStatusCode()); assertEquals("value", Bytes.toString(ByteStreams.toByteArray(resp.getEntity().getContent()))); } get = new HttpGet(String.format(URL_TEMPLCATE, PORT, TABLE_NAME.getNameAsString(), "whatever", FAMILY, QUALIFIER)); try (CloseableHttpResponse resp = client.execute(get)) { assertEquals(HttpStatus.SC_NOT_FOUND, resp.getStatusLine().getStatusCode()); } } } }
private HttpResponse put(final String path, final Object item) throws IOException { final ObjectMapper mapper = new ObjectMapper(); final String json = mapper.writeValueAsString(item); final StringEntity entity = new StringEntity(json); final HttpPut request = new HttpPut(adminApiUrl + path); request.setHeader("Content-type", "application/json"); request.setEntity(entity); final HttpResponse response = httpClient.execute(request, httpContext); checkAuthFailure(response); return response; }
public static final int put(final HttpClient httpClient, final String url, final String body) throws IOException { final HttpPut put = new HttpPut(url); if (body != null) { put.setHeader("Content-Type", Constants.APPLICATION_JSON); put.setEntity(new StringEntity(body, "UTF-8")); } return httpClient.execute(put, new StatusCodeResponseHandler()); }
public TenantPort updateTenantVifBinding(String portUuid, String hypervisorIpAddress) { TenantPort req = new TenantPort(); if (hypervisorIpAddress != null) { req.attachmentType = "VifAttachment"; req.hypervisorIpAddress = hypervisorIpAddress; } else { req.attachmentType = "NoAttachment"; } HttpPut method = new HttpPut(); method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON)); return new Gson().fromJson( executeMethod(method, "/ssp.v1/tenant-ports/" + portUuid), TenantPort.class); } }
request = postRequest; }else if(HttpPut.METHOD_NAME.equalsIgnoreCase(httpVerb)){ HttpPut putRequest = new HttpPut(url); ContentType parsedContentType = ContentType.parse(contentType); StringEntity requestBody = new StringEntity(body, parsedContentType); putRequest.setEntity(requestBody); request = putRequest;
private HttpRequestBase constructRequestBase(HttpEntity theEntity) { String url = myUrl.toString(); switch (myRequestType) { case DELETE: return new HttpDelete(url); case PATCH: HttpPatch httpPatch = new HttpPatch(url); httpPatch.setEntity(theEntity); return httpPatch; case OPTIONS: return new HttpOptions(url); case POST: HttpPost httpPost = new HttpPost(url); httpPost.setEntity(theEntity); return httpPost; case PUT: HttpPut httpPut = new HttpPut(url); httpPut.setEntity(theEntity); return httpPut; case GET: default: return new HttpGet(url); } }
JsonObject httpPut(String url, String body, User registrar) throws Exception { String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "PUT", url, body); String putURL = addCAToURL(url); HttpPut httpPut = new HttpPut(putURL); httpPut.setConfig(getRequestConfig()); logger.debug(format("httpPutt %s, body:%s, authHTTPCert: %s", url, body, authHTTPCert)); final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (registry != null) { httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry)); } HttpClient client = httpClientBuilder.build(); final HttpClientContext context = HttpClientContext.create(); httpPut.setEntity(new StringEntity(body)); httpPut.addHeader("Authorization", authHTTPCert); HttpResponse response = client.execute(httpPut, context); return getResult(response, body, "PUT"); }