@Override public void doWithRequest(ClientHttpRequest request) throws IOException { request.getHeaders().putAll(this.headers); if (this.body != null) { StreamUtils.copy(this.body, SockJsFrame.CHARSET, request.getBody()); } } }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { this.request.getHeaders().putAll(headers); StreamUtils.copy(bufferedOutput, this.request.getBody()); ClientHttpResponse response = this.request.execute(); return new BufferingClientHttpResponseWrapper(response); }
final InputStream fis = new FileInputStream(new File("c:\\autoexec.bat")); // or whatever final RequestCallback requestCallback = new RequestCallback() { @Override public void doWithRequest(final ClientHttpRequest request) throws IOException { request.getHeaders().add("Content-type", "application/octet-stream"); IOUtils.copy(fis, request.getBody()); } }; final RestTemplate restTemplate = new RestTemplate(); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setBufferRequestBody(false); restTemplate.setRequestFactory(requestFactory); final HttpMessageConverterExtractor<String> responseExtractor = new HttpMessageConverterExtractor<String>(String.class, restTemplate.getMessageConverters()); restTemplate.execute("http://localhost:4000", HttpMethod.POST, requestCallback, responseExtractor);
@Test(expected = IllegalStateException.class) public void multipleWrites() throws Exception { ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/echo"), HttpMethod.POST); final byte[] body = "Hello World".getBytes("UTF-8"); if (request instanceof StreamingHttpOutputMessage) { StreamingHttpOutputMessage streamingRequest = (StreamingHttpOutputMessage) request; streamingRequest.setBody(outputStream -> { StreamUtils.copy(body, outputStream); outputStream.flush(); outputStream.close(); }); } else { StreamUtils.copy(body, request.getBody()); } request.execute(); FileCopyUtils.copy(body, request.getBody()); }
@Test(expected = UnsupportedOperationException.class) public void headersAfterExecute() throws Exception { ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/status/ok"), HttpMethod.POST); request.getHeaders().add("MyHeader", "value"); byte[] body = "Hello World".getBytes("UTF-8"); FileCopyUtils.copy(body, request.getBody()); ClientHttpResponse response = request.execute(); try { request.getHeaders().add("MyHeader", "value"); } finally { response.close(); } }
@Override public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException { if (this.iterator.hasNext()) { ClientHttpRequestInterceptor nextInterceptor = this.iterator.next(); return nextInterceptor.intercept(request, body, this); } else { HttpMethod method = request.getMethod(); Assert.state(method != null, "No standard HTTP method"); ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method); request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value)); if (body.length > 0) { if (delegate instanceof StreamingHttpOutputMessage) { StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate; streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream)); } else { StreamUtils.copy(body, delegate.getBody()); } } return delegate.execute(); } } }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { this.request.getHeaders().putAll(headers); StreamUtils.copy(bufferedOutput, this.request.getBody()); ClientHttpResponse response = this.request.execute(); return new BufferingClientHttpResponseWrapper(response); }
@Test @Ignore public void largeFileUpload() throws Exception { Random rnd = new Random(); ClientHttpResponse response = null; try { ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/post"), HttpMethod.POST); final int BUF_SIZE = 4096; final int ITERATIONS = Integer.MAX_VALUE / BUF_SIZE; // final int contentLength = ITERATIONS * BUF_SIZE; // request.getHeaders().setContentLength(contentLength); OutputStream body = request.getBody(); for (int i = 0; i < ITERATIONS; i++) { byte[] buffer = new byte[BUF_SIZE]; rnd.nextBytes(buffer); body.write(buffer); } response = request.execute(); assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode()); } finally { if (response != null) { response.close(); } } }
protected void assertHttpMethod(String path, HttpMethod method) throws Exception { ClientHttpResponse response = null; try { ClientHttpRequest request = factory.createRequest(new URI(baseUrl + "/methods/" + path), method); if (method == HttpMethod.POST || method == HttpMethod.PUT || method == HttpMethod.PATCH) { // requires a body try { request.getBody().write(32); } catch (UnsupportedOperationException ex) { // probably a streaming request - let's simply ignore it } } response = request.execute(); assertEquals("Invalid response status", HttpStatus.OK, response.getStatusCode()); assertEquals("Invalid method", path.toUpperCase(Locale.ENGLISH), request.getMethod().name()); } finally { if (response != null) { response.close(); } } }
@Override public ClientHttpResponse execute(HttpRequest request, byte[] body) throws IOException { if (this.iterator.hasNext()) { ClientHttpRequestInterceptor nextInterceptor = this.iterator.next(); return nextInterceptor.intercept(request, body, this); } else { HttpMethod method = request.getMethod(); Assert.state(method != null, "No standard HTTP method"); ClientHttpRequest delegate = requestFactory.createRequest(request.getURI(), method); request.getHeaders().forEach((key, value) -> delegate.getHeaders().addAll(key, value)); if (body.length > 0) { if (delegate instanceof StreamingHttpOutputMessage) { StreamingHttpOutputMessage streamingOutputMessage = (StreamingHttpOutputMessage) delegate; streamingOutputMessage.setBody(outputStream -> StreamUtils.copy(body, outputStream)); } else { StreamUtils.copy(body, delegate.getBody()); } } return delegate.execute(); } } }
StreamUtils.copy(body, request.getBody());
byte[] body = "Hello World".getBytes("UTF-8"); request.getHeaders().setContentLength(body.length); FileCopyUtils.copy(body, request.getBody()); ClientHttpResponse response = request.execute(); try {
private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes) throws IOException { try { while (true) { try { ClientHttpRequest request = this.requestFactory .createRequest(this.uri, HttpMethod.POST); HttpHeaders headers = request.getHeaders(); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); headers.setContentLength(bytes.length); FileCopyUtils.copy(bytes, request.getBody()); ClientHttpResponse response = request.execute(); HttpStatus statusCode = response.getStatusCode(); Assert.state(statusCode == HttpStatus.OK, () -> "Unexpected " + statusCode + " response uploading class files"); logUpload(classLoaderFiles); return; } catch (ConnectException ex) { logger.warn("Failed to connect when uploading to " + this.uri + ". Upload will be retried in 2 seconds"); Thread.sleep(2000); } } } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new IllegalStateException(ex); } }
@Override protected OutputStream getRequestOutputStream() throws IOException { return request.getBody(); }
public ClientHttpResponse execute() throws IOException { byte[] bufferedOutput = bodyOutputStream.toByteArray(); delegate.getBody().write(bufferedOutput); delegate.getHeaders().set("x-li-format", "json"); return delegate.execute(); }
@Override public void doWithRequest(ClientHttpRequest request) throws IOException { copyHeaders(httpRequest, request); FileCopyUtils.copy(httpRequest.getInputStream(), request.getBody()); }
private ClientHttpResponse executeRequest(HttpMethod method, String url, MultiValueMap<String, String> body) throws IOException { ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory(); ClientHttpRequest request = requestFactory.createRequest(URI.create(url), method); OutputStreamWriter writer = new OutputStreamWriter(request.getBody()); writer.write(createFormUrlEncodedBodyString(body)); writer.flush(); request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); return request.execute(); }
private ClientHttpResponse executeRequest(HttpMethod method, String url, MultiValueMap<String, String> body) throws IOException { ClientHttpRequestFactory requestFactory = restTemplate.getRequestFactory(); ClientHttpRequest request = requestFactory.createRequest(URI.create(url), method); OutputStreamWriter writer = new OutputStreamWriter(request.getBody()); writer.write(createFormUrlEncodedBodyString(body)); writer.flush(); request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); return request.execute(); }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { OutputStream body = this.request.getBody(); this.request.getHeaders().putAll(headers); FileCopyUtils.copy(bufferedOutput, body); ClientHttpResponse response = this.request.execute(); return new BufferingClientHttpResponseWrapper(response); }
@Override protected ClientHttpResponse executeInternal(HttpHeaders headers, byte[] bufferedOutput) throws IOException { this.request.getHeaders().putAll(headers); StreamUtils.copy(bufferedOutput, this.request.getBody()); ClientHttpResponse response = this.request.execute(); return new BufferingClientHttpResponseWrapper(response); }