androidmozillageckoview

How do you add extra headers into request in Android GeckoView?


I found this thread on the Mozilla bug tracking website. It seems like the issue was addressed. However, when I go to import a recent version of the library from maven in Android Studio. Specifically version org.mozilla.geckoview:geckoview-nightly:100.0.20220308100756.

The only method signature I am able to use is GeckoSession.loadUri(uri). The desired GeckoSession.loadUri(uri, extraHeaders) is nowhere to be found. Anyone have any insights into this? I am afraid that the documentation is quite sparse around here.


Solution

  • To achieve this I make use of GeckoSessions' Loader, which allows you to use a Builder-like format to pass in values.

    To do this, I'd write:

    Map <String, String> extraHeaders = Map.of("Key 1", "Value 1", "Key 2", "Value 2", etc.);
    
    GeckoSession.Loader loader = new GeckoSession.Loader();
    loader.additionalHeaders(extraHeaders);
    loader.uri("https://example.com");
    
    geckoSession.load(loader); 
    

    Worth noting that theoretically you shouldn't need to assign each method to the loader like this and instead use a more conventional Builder-like format, but in my experience GeckoSession seems to fail at loading in https headers when you don't have them already declared in a Map before passing, so this is easiest.