Private eventsource endpoints, passing headers to clients

Hey, a good day for everyone.

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, 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?

Crates:

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);

It can also be done in reqwest-eventsource by using a RequestBuilder:

use reqwest::{header::HeaderMap, Client};
use reqwest_eventsource::RequestBuilderExt;

let mut headers = HeaderMap::new();
headers.insert("Authorization", "Basic Zm9vOmJhcg==".parse()?);
headers.insert("X-Foo", "bar".parse()?);
let client = Client::builder().default_headers(headers).build()?;
let event_source = client.get("https://example.com/").eventsource()?;

/* or alternatively: */

use reqwest::Client;
use reqwest_eventsource::RequestBuilderExt;

let event_source = Client::new()
    .get("https://example.com/")
    .header("Authorization", "Basic Zm9vOmJhcg==")
    .header("X-Foo", "bar")
    .eventsource()?;
1 Like

Fantastic, I was looking for this new_with_client but wasn't aware of the ReqwestClient::builder().default_headers(headers), sounds great tho!

@LegionMammal978, looking toward these libs, do you have a guess what is better, mainly for performance and stability?

Personally, I'd probably prefer reqwest-eventsource, since it is the more recently updated of the two, and has not too many fewer recent downloads.

1 Like

Awesome, thanks for your help!