private RequestEntity<?> preparePost(String url, HttpHeaders headers, Object body) throws Exception { URI uri = new URI("http://localhost:" + this.port + url); RequestEntity.BodyBuilder builder = post(uri); addHeaders(builder, headers); return builder.body(body); }
@Test public void checkUri() throws Exception { URI url = new URI("http://localhost:" + port + "/foo?param=bar"); RequestEntity<Void> request = RequestEntity.post(url).build(); ResponseEntity<Void> response = new RestTemplate().exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); }
@Test public void checkUri() throws Exception { URI url = new URI("https://localhost:" + port + "/foo?param=bar"); RequestEntity<Void> request = RequestEntity.post(url).build(); ResponseEntity<Void> response = this.restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); }
@Test public void writeOnly() throws Exception { RestTemplate restTemplate = new RestTemplate(); this.body = randomBytes(); RequestEntity<byte[]> request = RequestEntity.post( new URI("http://localhost:" + port)).body( "".getBytes(StandardCharsets.UTF_8)); ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class); assertArrayEquals(body, response.getBody()); }
@Test public void echo() throws Exception { RestTemplate restTemplate = new RestTemplate(); byte[] body = randomBytes(); RequestEntity<byte[]> request = RequestEntity.post(new URI("http://localhost:" + port)).body(body); ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class); assertArrayEquals(body, response.getBody()); }
@Test public void getFormParts() throws Exception { RestTemplate restTemplate = new RestTemplate(); RequestEntity<MultiValueMap<String, Object>> request = RequestEntity .post(new URI("http://localhost:" + port + "/form-parts")) .contentType(MediaType.MULTIPART_FORM_DATA) .body(generateBody()); ResponseEntity<Void> response = restTemplate.exchange(request, Void.class); assertEquals(HttpStatus.OK, response.getStatusCode()); }
@EventListener @Async public void handleEvent(ProjectRequestEvent event) { String json = null; try { ProjectRequestDocument document = this.documentFactory.createDocument(event); if (log.isDebugEnabled()) { log.debug("Publishing " + document); } json = toJson(document); RequestEntity<String> request = RequestEntity.post(this.requestUrl) .contentType(MediaType.APPLICATION_JSON).body(json); this.retryTemplate.execute((context) -> { this.restTemplate.exchange(request, String.class); return null; }); } catch (Exception ex) { log.warn(String.format( "Failed to publish stat to index, document follows %n%n%s%n", json), ex); } }
@Test public void postMono() { URI uri = URI.create("http://localhost:" + port + "/mono"); Person person = new Person("Jack"); RequestEntity<Person> requestEntity = RequestEntity.post(uri).body(person); ResponseEntity<Person> result = restTemplate.exchange(requestEntity, Person.class); assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals("Jack", result.getBody().getName()); }
@Test // SPR-13154 public void jsonPostForObjectWithJacksonTypeInfoList() throws URISyntaxException { List<ParentClass> list = new ArrayList<>(); list.add(new Foo("foo")); list.add(new Bar("bar")); ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<ParentClass>>() {}; RequestEntity<List<ParentClass>> entity = RequestEntity .post(new URI(baseUrl + "/jsonpost")) .contentType(new MediaType("application", "json", StandardCharsets.UTF_8)) .body(list, typeReference.getType()); String content = template.exchange(entity, String.class).getBody(); assertTrue(content.contains("\"type\":\"foo\"")); assertTrue(content.contains("\"type\":\"bar\"")); }
@Test public void random() throws Throwable { // TODO: fix Reactor support RestTemplate restTemplate = new RestTemplate(); byte[] body = randomBytes(); RequestEntity<byte[]> request = RequestEntity.post(new URI("http://localhost:" + port)).body(body); ResponseEntity<byte[]> response = restTemplate.exchange(request, byte[].class); assertNotNull(response.getBody()); assertEquals(RESPONSE_SIZE, response.getHeaders().getContentLength()); assertEquals(RESPONSE_SIZE, response.getBody().length); }
public ResponseEntity<T> post() { RequestEntity<Object> requestEntity = headers(RequestEntity.post(uri)) .body(body()); return exchange(requestEntity); }
public Mono<ResponseEntity<T>> post() { RequestEntity<Object> requestEntity = headers(RequestEntity.post(uri)) .body(body()); return exchange(requestEntity); }
@Test // SPR-13154 public void types() throws URISyntaxException { URI url = new URI("http://example.com"); List<String> body = Arrays.asList("foo", "bar"); ParameterizedTypeReference<?> typeReference = new ParameterizedTypeReference<List<String>>() {}; RequestEntity<?> entity = RequestEntity.post(url).body(body, typeReference.getType()); assertEquals(typeReference.getType(), entity.getType()); }
@Test // SPR-13319 public void standardMultipartResolverWithEncodedFileName() throws Exception { byte[] boundary = MimeTypeUtils.generateMultipartBoundary(); String boundaryText = new String(boundary, "US-ASCII"); Map<String, String> params = Collections.singletonMap("boundary", boundaryText); String content = "--" + boundaryText + "\n" + "Content-Disposition: form-data; name=\"file\"; filename*=\"utf-8''%C3%A9l%C3%A8ve.txt\"\n" + "Content-Type: text/plain\n" + "Content-Length: 7\n" + "\n" + "content\n" + "--" + boundaryText + "--"; RequestEntity<byte[]> requestEntity = RequestEntity.post(new URI(baseUrl + "/standard-resolver/spr13319")) .contentType(new MediaType(MediaType.MULTIPART_FORM_DATA, params)) .body(content.getBytes(StandardCharsets.US_ASCII)); ByteArrayHttpMessageConverter converter = new ByteArrayHttpMessageConverter(); converter.setSupportedMediaTypes(Collections.singletonList(MediaType.MULTIPART_FORM_DATA)); this.restTemplate.setMessageConverters(Collections.singletonList(converter)); ResponseEntity<Void> responseEntity = restTemplate.exchange(requestEntity, Void.class); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); }
@Test public void headers() throws URISyntaxException { MediaType accept = MediaType.TEXT_PLAIN; long ifModifiedSince = 12345L; String ifNoneMatch = "\"foo\""; long contentLength = 67890; MediaType contentType = MediaType.TEXT_PLAIN; RequestEntity<Void> responseEntity = RequestEntity.post(new URI("http://example.com")). accept(accept). acceptCharset(StandardCharsets.UTF_8). ifModifiedSince(ifModifiedSince). ifNoneMatch(ifNoneMatch). contentLength(contentLength). contentType(contentType). build(); assertNotNull(responseEntity); assertEquals(HttpMethod.POST, responseEntity.getMethod()); assertEquals(new URI("http://example.com"), responseEntity.getUrl()); HttpHeaders responseHeaders = responseEntity.getHeaders(); assertEquals("text/plain", responseHeaders.getFirst("Accept")); assertEquals("utf-8", responseHeaders.getFirst("Accept-Charset")); assertEquals("Thu, 1 Jan 1970 00:00:12 GMT", responseHeaders.getFirst("If-Modified-Since")); assertEquals(ifNoneMatch, responseHeaders.getFirst("If-None-Match")); assertEquals(String.valueOf(contentLength), responseHeaders.getFirst("Content-Length")); assertEquals(contentType.toString(), responseHeaders.getFirst("Content-Type")); assertNull(responseEntity.getBody()); }
@Test public void methods() throws URISyntaxException { URI url = new URI("http://example.com"); RequestEntity<?> entity = RequestEntity.get(url).build(); assertEquals(HttpMethod.GET, entity.getMethod()); entity = RequestEntity.post(url).build(); assertEquals(HttpMethod.POST, entity.getMethod()); entity = RequestEntity.head(url).build(); assertEquals(HttpMethod.HEAD, entity.getMethod()); entity = RequestEntity.options(url).build(); assertEquals(HttpMethod.OPTIONS, entity.getMethod()); entity = RequestEntity.put(url).build(); assertEquals(HttpMethod.PUT, entity.getMethod()); entity = RequestEntity.patch(url).build(); assertEquals(HttpMethod.PATCH, entity.getMethod()); entity = RequestEntity.delete(url).build(); assertEquals(HttpMethod.DELETE, entity.getMethod()); }
@Override public Team createTeam(String organization, TeamRequest teamRequest) { Map<String, Object> uriVariables = new HashMap<>(); uriVariables.put("organization", organization); RequestEntity<TeamRequest> entity = RequestEntity.post(buildUri("/orgs/{organization}/teams", uriVariables)) .contentType(MediaType.APPLICATION_JSON).body(teamRequest); return getRestTemplate().exchange(entity, Team.class).getBody(); }
@Override public Issue createIssue(IssueRequest issueRequest, String owner, String repo) { Map<String, Object> uriVariables = new HashMap<>(); uriVariables.put("owner", owner); uriVariables.put("repo", repo); URI uri = new UriTemplate(buildUriString("/repos/{owner}/{repo}/issues")).expand(uriVariables); RequestEntity<IssueRequest> entity = RequestEntity.post(uri).contentType(MediaType.APPLICATION_JSON) .body(issueRequest); ResponseEntity<Issue> responseEntity = getRestTemplate().exchange(entity, Issue.class); return responseEntity.getBody(); }
@Override public List<Email> addEmails(List<String> emails) { RequestEntity<List<String>> reqEntity = RequestEntity.post(buildUri(USER_EMAILS_PATH)) .contentType(MediaType.APPLICATION_JSON).body(emails); return getRestTemplate().exchange(reqEntity, emailListTypeRef).getBody(); }
@Test public void shouldBeAbleToMatchBodyByXPath() throws Exception { // Given final RequestEntity<String> bookFlightRequest = RequestEntity.post(new URI("http://www.my-test.com/api/bookings")) .contentType(APPLICATION_JSON) .body("<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <flightId>1</flightId>"); // When final ResponseEntity<String> bookFlightResponse = restTemplate.exchange(bookFlightRequest, String.class); // Then assertThat(bookFlightResponse.getStatusCode()).isEqualTo(CREATED); assertThat(bookFlightResponse.getHeaders().getLocation()).isEqualTo(new URI("http://localhost/api/bookings/1")); }