Created
October 29, 2015 18:35
-
-
Save anna-is-cute/16fe8e04eab2c230f1ce to your computer and use it in GitHub Desktop.
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
// Kotlin + HttpURLConnection | |
val connection = URL("https://api.github.com/user").openConnection() as HttpURLConnection | |
val auth = Base64.getEncoder().encode("user:pass".toByteArray()).toString(Charsets.UTF_8) | |
connection.addRequestProperty("Authorization", "Basic $auth") | |
connection.connect() | |
println(connection.responseCode) | |
println(connection.getHeaderField("Content-Type")) | |
val text = connection.inputStream.use { it.reader().use { reader -> reader.readText() } } | |
println(text) | |
println(JSONObject(text)) | |
// khttp | |
val r = get("https://api.github.com/user", auth = BasicAuthorization("user", "pass")) | |
println(r.statusCode) | |
println(r.headers["content-type"]) | |
println(r.text) | |
println(r.jsonObject) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment