Add arbitrary request headers to EhttpLoader#8121
Conversation
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`.
|
Preview available at https://egui-pr-preview.github.io/pr/8121-http-loader-headers View snapshot changes at kitdiff |
|
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. 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
})
)); |
modified e.g. per url
instead of a mutable ref
Yes that's a good point. I updated the PR to do basically what you suggested. But I pass in the prepared |
Lets you create an
EhttpLoaderwith arbitrary headers like so:I'm not sure if there are any problems with installing a second
EhttpLoader(in addition to the one installed by default when usingegui_extras::install_image_loaders(&cc.egui_ctx). But I wasn'tsure how else to pass in configuration options to
install_image_loaders.