Search code examples
androidkotlinhttp-headersandroid-webview

How to add authorization bearer header to webview android?


I have to send bearer at headers. I saw that I have to add hashMap with values:

val headerMap = HashMap<String, String>()
headerMap["Authorization: Bearer "] = context!!.getSharedPreferences("app_data", 0).getString("access_token", "")!!

and then send data with url:

webView.loadUrl(link, headerMap)

but as a result I see that I send the wrong format of this token:

authorization=bearer :token

How I can fix it because with that token I can't get data from page?


Solution

  • Can you try to do it this way

    val bearer = "Bearer " + context!!.getSharedPreferences("app_data", 0).getString("access_token", "")!!
    
    val headerMap = HashMap<String,String>()
    headerMap["Authorization"] = bearer
    webView.loadUrl(link, headerMap)
    

    You need to think, you are using a HashMap so, means it has a Key and a Value, Key is the Header name and then the Value is the value of that Header name so in this case is :

    Header name --> Authorization

    Header value --> Bearer <your_access_token>