From my Android/Kotlin app I need to make requests to a backend REST API. I need to send along a JWT for auth. I'm currently using code like this that I shamelessly copied from this answer: https://stackoverflow.com/a/46179139/3267158
private fun sendGet() {
val url = "http://www.google.com/"
val obj = URL(url)
with(obj.openConnection() as HttpURLConnection) {
// optional default is GET
requestMethod = "GET"
println("\nSending 'GET' request to URL : $url")
println("Response Code : $responseCode")
BufferedReader(InputStreamReader(inputStream)).use {
val response = StringBuffer()
var inputLine = it.readLine()
while (inputLine != null) {
response.append(inputLine)
inputLine = it.readLine()
}
println(response.toString())
}
}
}
But I'm not married to this code if there is a better way using facilities readily available in Android/Kotlin.
Can someone show me some simple code that makes an HTTP PUT or GET request and including a JWT in the header.