Adding request header to cordova webview

To add headers while making request to URL through Cordova WebView following steps has to be followed:

  1. Create a HashMap and add the header parameters to this HashMap
  2. Pass the HashMap to WebView’s loadUrl function along with the URL
  3. In shouldOverrideUrlLoading method of CordovaWebViewClient or WebViewClient add the same HashMap to WebView’s loadUrl function along with the URL

Creating a HashMap:

Map<String, String> map = new HashMap<String, String>();

map.put(“header_name”, “value”);

Passing the header value while calling loadUrl:

loadUrl(url, map);

Passing the same headers in shouldOverrideUrlLoading:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url, map);
return true;
}

Note: The HashMap which contains the headers should be passed while calling loadUrl of WebView as well as shouldOverrideUrlLoading of WebViewClient.

Advertisements