0

I want to add my header to each request in webview but I dont want recreate the request using OkHttp or DefaultHttpClient since WebResourceRequest from shouldInterceptRequest does not return the request body so my POST requests wont have value when i send it.

I tried adding my header inside shouldInterceptRequest but i doesnt work properly seems its not adding on each request.

  override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
            request?.requestHeaders?.clear()
            request?.requestHeaders?.apply {
                put("header", "value") 
            }
            return super.shouldInterceptRequest(view, request)
        }
Renz Manacmol
  • 869
  • 12
  • 27

1 Answers1

0

Can you try passing the header values via the view: WebView param using:

public void loadUrl(@NonNull String url, @NonNull Map<String, String> additionalHttpHeaders) {

so something like:

  override fun shouldInterceptRequest(view: WebView?, request: WebResourceRequest?): WebResourceResponse? {
    view.loadUrl("your url", mapOf("header" to "value"))
    return super.shouldInterceptRequest(view, request)
  }
D Ta
  • 864
  • 6
  • 17
  • that is giving me this error. 'A WebView method was called on thread 'ThreadPoolForeg'. All WebView methods must be called on the same thread.' – Renz Manacmol May 04 '23 at 08:24