I have an endpoint which sends out server side events. For testing purposes I have made the endpoint deterministic and the connection to the endpoint terminates after 30 seconds. If I curl the endpoint I get all the data as expected:
$ curl "my/end/point"
data: {"message": "event1"}
data: {"message": "event2"}
data: {"message": "event3"}
data: {"message": "event4"}
...
However, when I use Springs WebClient no data is received. The code I use:
final WebClient webClient = WebClient.create("my/end/point");
final String result = webClient.get()
.retrieve()
.bodyToFlux(String.class)
.blockFirst();
System.out.println(result);
When I run this code I get the following result after 30 seconds:
null
The response code from the request is a 200 OK.
The following example
final StringBuilder result = new StringBuilder();
final URL url = new URL("my/end/point");
final URLConnection conn = url.openConnection();
final BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
rd.close();
System.out.println(result.toString());
does show the expected result after 30 seconds when run
data: {"message": "event1"}
data: {"message": "event2"}
...
Does anybody know why the result from the WebClient is empty while other methods of fetching the data from the endpoint are not? I'm using java 11 and spring-webflux version 5.1.5.