Simple function to Http GET some data
The core of your code can be replaced by the following code:

url.openConnection()    //you can cast to fail if it isn't https
   .getInputStream()   //same as you
   .reader()          // same as wrapping it in inputStreamReader
   .use { it -&gt;      // start lambda that handles the closing of the reader for you
      it.readText() // note, it's one parameter, so 'it -&gt;' can be omitted
    }


The code above returns the text or throws the exception it got during execution, but always closes the reader, which in turn closes the connection.

The code above is almost the same as the code below btw:

url.readText()


If you want to remove all the variables, you should know that try catch can return a value:

val a = try{ 1 } catch(e: Exception) { 2 }