Skip to content

Add arbitrary request headers to EhttpLoader#8121

Open
frnsys wants to merge 5 commits into
emilk:mainfrom
frnsys:http-loader-headers
Open

Add arbitrary request headers to EhttpLoader#8121
frnsys wants to merge 5 commits into
emilk:mainfrom
frnsys:http-loader-headers

Conversation

@frnsys
Copy link
Copy Markdown

@frnsys frnsys commented Apr 20, 2026

Lets you create an EhttpLoader with arbitrary headers like so:

cc.egui_ctx.add_bytes_loader(std::sync::Arc::new(
    egui_extras::loaders::http_loader::EhttpLoader::default().with_headers(&[
        ("User-Agent", "foo"),
    ])
));

I'm not sure if there are any problems with installing a second
EhttpLoader (in addition to the one installed by default when using
egui_extras::install_image_loaders(&cc.egui_ctx). But I wasn't
sure how else to pass in configuration options to
install_image_loaders.

frnsys added 2 commits April 20, 2026 22:21
May close emilk#4491

Lets you create an `EhttpLoader` with arbitrary headers like so:

```rust
cc.egui_ctx.add_bytes_loader(std::sync::Arc::new(
    egui_extras::loaders::http_loader::EhttpLoader::default().with_headers(&[
        ("User-Agent", "foo"),
    ])
));
```

I'm not sure if there are any problems with installing a second
`EhttpLoader` (in addition to the one installed by default when using
`egui_extras::install_image_loaders(&cc.egui_ctx)`. But I didn't wasn't
sure how else to pass in configuration options to
`install_image_loaders`.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Apr 20, 2026

Preview available at https://egui-pr-preview.github.io/pr/8121-http-loader-headers
Note that it might take a couple seconds for the update to show up after the preview_build workflow has completed.

View snapshot changes at kitdiff

@lucasmerlin
Copy link
Copy Markdown
Collaborator

If someone wanted to use this for authentication, they'd have to ensure they only ever load images from trusted endpoints, so their token is not exposed to any third partys.
Maybe instead we could have a request_template closure arg? Then one could do something like this:

cc.egui_ctx.add_bytes_loader(std::sync::Arc::new(
    egui_extras::loaders::http_loader::EhttpLoader::default().with_request_template(|url| {
		let mut request = ehttp::Request::get(url);
		if url.starts_with("https://my-api.com") {
		    request = request.with_headers(&[
        		("User-Agent", "foo"),
    		])
		}
		request
	})
));

@emilk emilk added feature New feature or request egui_extras labels Apr 22, 2026
@frnsys
Copy link
Copy Markdown
Author

frnsys commented Apr 22, 2026

If someone wanted to use this for authentication, they'd have to ensure they only ever load images from trusted endpoints, so their token is not exposed to any third partys. Maybe instead we could have a request_template closure arg? Then one could do something like this:

cc.egui_ctx.add_bytes_loader(std::sync::Arc::new(
    egui_extras::loaders::http_loader::EhttpLoader::default().with_request_template(|url| {
		let mut request = ehttp::Request::get(url);
		if url.starts_with("https://my-api.com") {
		    request = request.with_headers(&[
        		("User-Agent", "foo"),
    		])
		}
		request
	})
));

Yes that's a good point. I updated the PR to do basically what you suggested. But I pass in the prepared Request instead of the url in case for whatever reason the Request ends up being more complicated to construct.

Copy link
Copy Markdown
Collaborator

@lucasmerlin lucasmerlin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

egui_extras feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a User-Agent header to HTTP image loader

3 participants