authenticationhttprustserver-sent-events

How to consume from an eventsource API requiring auth headers?


I'm in a need of consuming an eventsource API, but it's a private one, where I need to pass some auth headers, specifically the Authorization one, and some more.

I had a look into these two crates (eventsource and reqwest_eventsource), but neither of them explains how to pass some headers. Has someone passed by something related or knows how to set it up in these crates?


Solution

  • This can be done in eventsource with Client::new_with_client():

    use eventsource::reqwest::Client as EventSourceClient;
    use reqwest::{blocking::Client as ReqwestClient, header::HeaderMap};
    
    let mut headers = HeaderMap::new();
    headers.insert("Authorization", "Basic Zm9vOmJhcg==".parse()?);
    headers.insert("X-Foo", "bar".parse()?);
    let client = ReqwestClient::builder().default_headers(headers).build()?;
    let client = EventSourceClient::new_with_client("https://example.com/".parse()?, client);