/** Set the request body as JSON. */ public HttpRequestBuilder withJsonContent(String content) { return withContent("application/json", content); }
/** Set the request body. */ public HttpRequestBuilder withContent(String type, byte[] content) { addHeader("Content-Type", type); entity = content; return this; }
/** * Compress the request body using the default compression level. * The content must have already been set on the builder. */ public HttpRequestBuilder compress() throws IOException { return compress(Deflater.DEFAULT_COMPRESSION); }
@Test public void okWithCompressedBody() throws IOException { HttpResponse res = HttpClient.DEFAULT .post(uri("/echo")) .acceptGzip() .addHeader("X-Status", "200") .withContent("text/plain", "foo") .compress() .send() .decompress(); Assertions.assertEquals(200, res.status()); Assertions.assertEquals("foo", res.entityAsString()); } }
try { HttpClient.DEFAULT.newRequest("spectator-reg-atlas", URI.create(config.uri())) .withMethod("POST") .withConnectTimeout((int) config.connectTimeout().toMillis()) .withReadTimeout((int) config.readTimeout().toMillis()) .withContent("application/x-jackson-smile", smileMapper.writeValueAsBytes(p)) .send(); } catch (IOException e) { logger.error("Unable to publish Atlas metrics", e);
@Test public void okWithBody() throws IOException { HttpResponse res = HttpClient.DEFAULT .post(uri("/echo")) .addHeader("X-Status", "200") .withContent("text/plain", "foo") .send(); Assertions.assertEquals(200, res.status()); Assertions.assertEquals("foo", res.entityAsString()); }
/** * Create a new POST request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder post(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("POST"); }
con.setRequestProperty(h.getKey(), h.getValue()); configureHTTPS(con); byte[] data = readAll(in); entry.withResponseContentLength(data.length); return new HttpResponse(status, headers, data);
/** Sets the policy used to verify hostnames when using HTTPS. */ public HttpRequestBuilder withHostnameVerifier(HostnameVerifier verifier) { requireHttps("hostname verification cannot be used with http, switch to https"); this.hostVerifier = verifier; return this; }
private void retryStatus(HttpResponse expectedRes, int expectedAttempts) throws IOException { AtomicInteger attempts = new AtomicInteger(); HttpResponseSupplier supplier = () -> { attempts.incrementAndGet(); return expectedRes; }; HttpResponse res = new TestRequestBuilder(supplier) .withInitialRetryDelay(0L) .withRetries(2) .send(); Assertions.assertEquals(expectedAttempts, attempts.get()); Assertions.assertEquals(expectedRes.status(), res.status()); }
@Test public void retry2() { AtomicInteger attempts = new AtomicInteger(); boolean failed = false; try { HttpResponseSupplier supplier = () -> { attempts.incrementAndGet(); throw new IOException("failed"); }; new TestRequestBuilder(supplier).withRetries(2).send(); } catch (IOException e) { failed = true; } Assertions.assertEquals(3, attempts.get()); Assertions.assertTrue(failed); }
/** Add header to accept-encoding of gzip. */ public HttpRequestBuilder acceptGzip() { return acceptEncoding("gzip"); }
entry.withAttempt(attempt); try { response = sendImpl(); int s = response.status(); if (s == 429 || s == 503) {
/** * Create a new GET request builder. The client name will be selected based * on a prefix of the host name. * * @param uri * URI to use for the request. * @return * Builder for creating and executing a request. */ default HttpRequestBuilder get(URI uri) { return newRequest(HttpUtils.clientNameForURI(uri), uri).withMethod("GET"); }
con.setRequestProperty(h.getKey(), h.getValue()); configureHTTPS(con); byte[] data = readAll(in); entry.withResponseContentLength(data.length); return new HttpResponse(status, headers, data);
/** Sets the socket factory to use with HTTPS. */ public HttpRequestBuilder withSSLSocketFactory(SSLSocketFactory factory) { requireHttps("ssl cannot be used with http, use https"); this.sslFactory = factory; return this; }
@Test public void hostnameVerificationWithHTTPS() throws IOException { HttpResponse res = new TestRequestBuilder(() -> OK, URI.create("https://foo.com/path")) .allowAllHosts() .send(); Assertions.assertEquals(200, res.status()); }
/** Add header to accept-encoding of gzip. */ public HttpRequestBuilder acceptGzip() { return acceptEncoding("gzip"); }