@Test public void multipart() throws UnsupportedEncodingException { MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("name 1", "value 1"); parts.add("name 2", "value 2+1"); parts.add("name 2", "value 2+2"); Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); parts.add("logo", logo); template.postForLocation(baseUrl + "/multipart", parts); }
@Test public void form() throws UnsupportedEncodingException { MultiValueMap<String, String> form = new LinkedMultiValueMap<>(); form.add("name 1", "value 1"); form.add("name 2", "value 2+1"); form.add("name 2", "value 2+2"); template.postForLocation(baseUrl + "/form", form); }
private void testCreate(String url, String basename) { MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("json-data", new HttpEntity<>(new TestData(basename))); parts.add("file-data", new ClassPathResource("logo.jpg", getClass())); parts.add("empty-data", new HttpEntity<>(new byte[0])); // SPR-12860 HttpHeaders headers = new HttpHeaders(); headers.setContentType(new MediaType("application", "octet-stream", StandardCharsets.ISO_8859_1)); parts.add("iso-8859-1-data", new HttpEntity<>(new byte[] {(byte) 0xC4}, headers)); // SPR-13096 URI location = restTemplate.postForLocation(url, parts); assertEquals("http://localhost:8080/test/" + basename + "/logo.jpg", location.toString()); }
@Test public void postForLocation() throws URISyntaxException { URI location = template.postForLocation(baseUrl + "/{method}", helloWorld, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), location); }
@Test public void postForLocationEntity() throws URISyntaxException { HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(new MediaType("text", "plain", StandardCharsets.ISO_8859_1)); HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders); URI location = template.postForLocation(baseUrl + "/{method}", entity, "post"); assertEquals("Invalid location", new URI(baseUrl + "/post/1"), location); }
@Test public void partListBinding() { PartListBean bean = new PartListBean(); partListServlet.setBean(bean); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); parts.add("partList", "first value"); parts.add("partList", "second value"); Resource logo = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); parts.add("partList", logo); template.postForLocation(baseUrl + "/partlist", parts); assertNotNull(bean.getPartList()); assertEquals(parts.get("partList").size(), bean.getPartList().size()); }
@Test public void partsBinding() { PartsBean bean = new PartsBean(); partsServlet.setBean(bean); MultiValueMap<String, Object> parts = new LinkedMultiValueMap<>(); Resource firstPart = new ClassPathResource("/org/springframework/http/converter/logo.jpg"); parts.add("firstPart", firstPart); parts.add("secondPart", "secondValue"); template.postForLocation(baseUrl + "/parts", parts); assertNotNull(bean.getFirstPart()); assertNotNull(bean.getSecondPart()); }
@Test public void postForLocationEntityContentType() throws Exception { mockSentRequest(POST, "http://example.com"); mockTextPlainHttpMessageConverter(); mockResponseStatus(HttpStatus.OK); String helloWorld = "Hello World"; HttpHeaders responseHeaders = new HttpHeaders(); URI expected = new URI("http://example.com/hotels"); responseHeaders.setLocation(expected); given(response.getHeaders()).willReturn(responseHeaders); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.setContentType(MediaType.TEXT_PLAIN); HttpEntity<String> entity = new HttpEntity<>(helloWorld, entityHeaders); URI result = template.postForLocation("http://example.com", entity); assertEquals("Invalid POST result", expected, result); verify(response).close(); }
@Test public void postForLocationEntityCustomHeader() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockTextPlainHttpMessageConverter(); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); URI expected = new URI("http://example.com/hotels"); responseHeaders.setLocation(expected); given(response.getHeaders()).willReturn(responseHeaders); HttpHeaders entityHeaders = new HttpHeaders(); entityHeaders.set("MyHeader", "MyValue"); HttpEntity<String> entity = new HttpEntity<>("Hello World", entityHeaders); URI result = template.postForLocation("http://example.com", entity); assertEquals("Invalid POST result", expected, result); assertEquals("No custom header set", "MyValue", requestHeaders.getFirst("MyHeader")); verify(response).close(); }
@Test public void postForLocationNull() throws Exception { HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); template.postForLocation("http://example.com", null); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void postForLocation() throws Exception { mockSentRequest(POST, "http://example.com"); mockTextPlainHttpMessageConverter(); mockResponseStatus(HttpStatus.OK); String helloWorld = "Hello World"; HttpHeaders responseHeaders = new HttpHeaders(); URI expected = new URI("http://example.com/hotels"); responseHeaders.setLocation(expected); given(response.getHeaders()).willReturn(responseHeaders); URI result = template.postForLocation("http://example.com", helloWorld); assertEquals("Invalid POST result", expected, result); verify(response).close(); }
@Test public void postForLocationNoLocation() throws Exception { mockSentRequest(POST, "http://example.com"); mockTextPlainHttpMessageConverter(); mockResponseStatus(HttpStatus.OK); URI result = template.postForLocation("http://example.com", "Hello World"); assertNull("Invalid POST result", result); verify(response).close(); }
@Override public URI postForLocation(String url, Object request, Object... urlVariables) throws RestClientException { return getRestTemplate(url).postForLocation(url, request, urlVariables); }
@Override public URI postForLocation(URI url, Object request) throws RestClientException { return getRestTemplate(url).postForLocation(url, request); }
@Override public URI postForLocation(String url, Object request, Map<String, ?> urlVariables) throws RestClientException { return getRestTemplate(url).postForLocation(url, request, urlVariables); }
@Override public void execute(RestTemplate restTemplate, String url, Object request) { restTemplate.postForLocation(url, request); } },
private void postAnswerSheet(String uri, Object body) { try { restTemplate.postForLocation(uri, body); } catch (RestClientException e) { LOGGER.error("Received exception:", e); } }
@Override public URI postForLocation(String url, Object object, String... params) { try { return restTemplate.postForLocation(url, object, params); } catch (RestClientException e) { throw new TrelloHttpException(e); } }
@Override public void createIndex(String index) throws IPFSStoreException { try { log.debug("createIndex [indexName={}]", index); restTemplate.postForLocation( this.endpoint + BASE_API_PATH + CREATE_INDEX_API_PATH + "/" + index, HttpEntity.EMPTY); log.debug("Index [indexName={}] created !", index); } catch (HttpClientErrorException ex) { throw handleHTTPExceptiion(ex); } }
@Override public void clearCompletedTasks(TaskList taskList) { notNull(taskList.getId()); restTemplate.postForLocation(TASKS_URL + taskList.getId() + "/clear", null); } }