Created
May 20, 2018 05:55
-
-
Save WLun001/ad99a4765a479edabaa035c4902fa76b to your computer and use it in GitHub Desktop.
HTTP request with authorisation token Kotlin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun httpRequestBuilder(url: String, request: Int): HttpsURLConnection { | |
val connection = URL(url).openConnection() as HttpsURLConnection | |
connection.readTimeout = 10000 | |
connection.connectTimeout = 15000 | |
connection.setRequestProperty("Authorization", "Bearer 234567834567") | |
when (request) { | |
GET -> { | |
connection.requestMethod = "GET" | |
connection.connect() | |
} | |
POST -> { | |
connection.setRequestProperty("Content-Type", "application/json") | |
connection.requestMethod = "POST" | |
connection.doOutput = true | |
connection.connect() | |
val bufferedWriter = BufferedWriter(OutputStreamWriter(connection.outputStream, "UTF-8")) | |
bufferedWriter.write(postData) | |
bufferedWriter.flush() | |
bufferedWriter.close() | |
connection.outputStream.close() | |
} | |
} | |
return connection | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment