Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional HTTP header with loading URL #816

Closed
1 of 2 tasks
realwakka opened this issue Dec 16, 2022 · 9 comments · Fixed by #805
Closed
1 of 2 tasks

Additional HTTP header with loading URL #816

realwakka opened this issue Dec 16, 2022 · 9 comments · Fixed by #805
Assignees
Labels
platform: All status: backlog Issue is ready and we can work on it status: in progress Implementation is proceeding smoothly type: feature request

Comments

@realwakka
Copy link
Contributor

I want to open a URL that needs additional custom http header. Is there any way to add custom http header?

It seems that android webview has a feature what I want.
https://developer.android.com/reference/android/webkit/WebView#loadUrl(java.lang.String,%20java.util.Map%3Cjava.lang.String,%20java.lang.String%3E)

It would be good to add some function for WebviewBuilder like below.


let _webview = WebViewBuilder::new(window)?
        .with_url(&some_url)?     
        .with_http_header("some", "header")
        .build()?;

I tried to add header with initialization_script but it failed.

Would you assign yourself to implement this feature?

  • Yes
  • No
@FabianLars
Copy link
Member

FabianLars commented Dec 27, 2022

It's supported on Linux too, a modified load_url method could look something like this:

pub fn load_url(&self, url: &str) {
    let req = URIRequest::builder().uri(url).build();
    let headers = req.http_headers().unwrap();
    headers.append(
      "My-Header",
      "This only works when you're creating new requests, you can't modify ones created by the webview itself.",
    );
    self.webview.load_request(&req);
  }

@amrbashir
Copy link
Member

http_headers from WebkitURIRequest is read-only.

@FabianLars
Copy link
Member

FabianLars commented Dec 27, 2022

I tested this before i sent the message:

grafik

I know where you're coming from tho, until now i only ever tried it for modifying requests from fetch for example which indeed doesn't work (unless one uses web extensions, super annoying imo)

@amrbashir
Copy link
Member

Oh nice, thanks @FabianLars. Apparently, I was looking at the wrong struct https://webkitgtk.org/reference/webkit2gtk/2.32.1/WebKitURIResponse.html

@amrbashir
Copy link
Member

@realwakka do you have an example we can use to test this?

@amrbashir amrbashir added status: in progress Implementation is proceeding smoothly status: backlog Issue is ready and we can work on it and removed status: waiting Waiting for a response or another PR status: upstream labels Dec 27, 2022
@amrbashir amrbashir self-assigned this Dec 27, 2022
@realwakka
Copy link
Contributor Author

Thanks! I just have tested the patch in feat/load-with-headers branch with this code below..

let webview = WebViewBuilder::new(window)?
        .with_url("https://tauri.studio")?
        .with_devtools(true)
        .build()?;


    let mut header = HeaderMap::new();
    header.append("X-CUSTOM-HEADER", HeaderValue::from_static("SOME_VALUE"));
    webview.load_url_with_headers("https://www.whatismybrowser.com/detect/what-http-headers-is-my-browser-sending", header);

And it works correctly!

image

I've tested in ubuntu which uses webkit2gtk.

@amrbashir
Copy link
Member

amrbashir commented Dec 31, 2022

thanks @realwakka for testing, it will be merged soon

@realwakka
Copy link
Contributor Author

@amrbashir Thanks for code!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: All status: backlog Issue is ready and we can work on it status: in progress Implementation is proceeding smoothly type: feature request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants