2

I am creating an application to read Event Stream and keeps the information on my database.

I have an URL: http://xx.xx.xx.xx:8080/restconf/streams/mno-vnf-event/json headers required: Accept:text/event-stream, Authorization:Basic YWRtaW46o99RtaW4-, Content-Type:application/yang-data+json

Now I want to fetch stream from above API using java (not javascript code), anyone has the idea to accomplish this.

nemequ
  • 16,623
  • 1
  • 43
  • 62

2 Answers2

2

You can use some Java libraries that supports SSE:

These are the first that come to my mind, but there are undoubtedly more.

ctranxuan
  • 785
  • 5
  • 9
  • Didn't find any option to add custom headers to make SSE request from Jersey or Spring WebClient. Want to add the following headers in the request: `Authorization:Basic YWRtaW46o99RtaW4-` `Accept:text/event-stream` `Content-Type:application/yang-data+json` – Abhishek Mishra Oct 23 '18 at 14:09
1

If the SSE server follows strictly the SSE spec, you can't add any custom headers.

Nonetheless, if it does, have you tried something like that with Spring WebFlux Client:

ResolvableType type = forClassWithGenerics(ServerSentEvent.class, String.class);

WebClient client = WebClient.create();
client.get()
      .uri("http://localhost:8080/sse")
      .accept(TEXT_EVENT_STREAM)
      .header("MyHeader", "Foo") // Add your headers here.
      .exchange()
      .flatMapMany(response -> response.body(toFlux(type)))
      .subscribe(System.out::println,
                 Throwable::printStackTrace);

I have tested this piece of code against a SSE Undertow server. It seems to work: Undertow is able to get the custom header that has been sent.

ctranxuan
  • 785
  • 5
  • 9