17

With standard Android WebViews, you use WebView.loadUrl(String url, Map<String, String> additionalHttpHeaders). How do you add your additional headers with Chrome Custom Tabs?

Matt Quigley
  • 7,614
  • 4
  • 25
  • 26

2 Answers2

25

I'm not sure if you can send headers or anything related to the http request besides the url. I hope future versions will allow sending headers in Bundle when you establish CustomTabSession or so.

Entire http call is managed in CustomTabActivity.

Edit:

As of recent updates of the library, you can now add Bundle and pass it to the CustomTabsIntent intent as extra with key Browser.EXTRA_HEADERS

Bundle headers = new Bundle();
headers.putString("header1", "value1");
headers.putString("header2", "value2");
customTabsIntent.intent.putExtra(Browser.EXTRA_HEADERS, headers);
Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • 4
    I want to specify a "Cookie" in my header. I tried doing `headers.putString("Cookie", sessionID);`. But when I inspect the request, it looks like maybe Chrome Custom Tabs overwrite my "Cookie" header with their own. Do you know if Chrome Custom Tabs overwrite the "Cookie" header? – B W Jan 18 '17 at 17:12
  • @Nikola Despotoski Sir I was trying to pass header using the above code mentioned by you but its not working please help how I am gonna rectify the problem. – Nikhil Singh May 03 '17 at 05:35
  • 5
    This seems to have stopped working with the latest version of Chrome. – Scott Kennedy Oct 16 '17 at 18:47
  • according to this article you can do this but it requires setting up Digital Asset Links: https://developers.google.com/web/android/custom-tabs/headers#set_up_a_custom_tabs_connection_to_validate_the_asset_link – WeezyKrush Dec 11 '20 at 22:36
  • `"The name of the extra data in the VIEW intent. The data are key/value pairs in the format of Bundle. They will be sent in the HTTP request headers for the provided url. The keys can't be the standard HTTP headers as they are set by the WebView. The url's schema must be http(s)."` For the documentation regarding `EXTRA_HEADERS`. – mr5 Jun 30 '22 at 01:15
1

The above mentions solutions won't work for new version of ChromeTab.Please follow this link fix for work around.You can also study the medium post by Romain Piel which implicitly states the usage and the work around for adding headers.

DRY Believer
  • 1,001
  • 11
  • 20
  • 3
    Neither of those links offer any solution that was not already mentioned above. – Scott Kennedy Nov 09 '17 at 01:26
  • 1
    Well i think there is'nt any work around for this because there seems to be a bug with chrometabs which implicitly states that there was a change in there version of chrometab after 13th Oct which states that they are using a wrong function of dispatch headers which only allows only on header to be added and ignores the headers when we add more than one headers and starts using its own conventional headers by overwriting the existing .Please follow this link ..https://bugs.chromium.org/p/chromium/issues/detail?id=772966 – DRY Believer Nov 09 '17 at 07:56
  • 1
    @DRYBeliever although your comments are true, your solution suggests there's a workaround, but neither link provides one. Please edit your answer to reflect this fact. – tresf Mar 11 '20 at 19:26