BAEL-4443 Reading HTTP ResponseBody as a String

This commit is contained in:
Oussama BEN MAHMOUD
2020-09-28 10:43:07 +02:00
parent 58896594d1
commit e134c020ce
6 changed files with 137 additions and 2 deletions
@@ -0,0 +1,26 @@
package com.baeldung.httpclient.readresponsebodystring;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import java.io.IOException;
public class ApacheHttpClientUnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test
public void whenUseApacheHttpClient_thenCorrect() throws IOException {
HttpGet request = new HttpGet(DUMMY_URL);
try (CloseableHttpClient client = HttpClients.createDefault(); CloseableHttpResponse response = client.execute(request)) {
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity);
System.out.println("Response -> " + result);
}
}
}
@@ -0,0 +1,29 @@
package com.baeldung.httpclient.readresponsebodystring;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class HttpClientUnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test
public void whenUseHttpClient_thenCorrect() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(DUMMY_URL)).build();
// synchronous response
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
// asynchronous response
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
}
@@ -0,0 +1,34 @@
package com.baeldung.httpclient.readresponsebodystring;
import org.junit.Assert;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpUrlConnectionUnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test
public void whenUseHttpUrlConnection_thenCorrect() throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(DUMMY_URL).openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder response = new StringBuilder();
String currentLine;
while ((currentLine = in.readLine()) != null)
response.append(currentLine);
in.close();
Assert.assertNotNull(response.toString());
System.out.println("Response -> " + response.toString());
}
}
@@ -0,0 +1,17 @@
package com.baeldung.httpclient.readresponsebodystring;
import org.junit.Test;
import org.springframework.web.client.RestTemplate;
public class SpringRestTemplateUnitTest {
public static final String DUMMY_URL = "https://postman-echo.com/get";
@Test
public void whenUseRestTemplate_thenCorrect() {
RestTemplate restTemplate = new RestTemplate();
String response = restTemplate.getForObject(DUMMY_URL, String.class);
System.out.println(response);
}
}