Created
April 22, 2021 21:54
-
-
Save dav1dddd/391be8d901023c020c240e5d87fc3670 to your computer and use it in GitHub Desktop.
send GET request to github lol
This file contains 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
import com.fasterxml.jackson.databind.ObjectMapper | |
import com.fasterxml.jackson.databind.SerializationFeature | |
import com.fasterxml.jackson.module.kotlin.readValue | |
import java.io.BufferedReader | |
import java.net.HttpURLConnection | |
import java.net.URL | |
import java.io.File | |
import kotlin.reflect.typeOf | |
class NewException(msg: String): Exception(msg) | |
fun GetRequest() { | |
val URI = URL("https://api.github.com/users") | |
with(URI.openConnection() as HttpURLConnection) { | |
requestMethod = "GET" | |
if (responseCode !== 200 || responseCode !== 301) { | |
NewException("Unable to make HTTP request!") | |
} | |
println("URL: $URI") | |
println("Response code: $responseCode") | |
inputStream.bufferedReader(charset("UTF-8")).use { | |
it.lines().forEach { line -> | |
// make sure JSON format is nice using Jackson library | |
// The ObjectMapper class is used to read and write JSON, that has a pretty printer enabled by default | |
// The SerializationFeature enum allows us to enable/disable features that affect how objects are | |
// serialized. | |
// I am enabling indentation for the JSON. | |
val mapper = ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT) | |
val json: Object = mapper.readValue(line) | |
println(mapper.writeValueAsString(json)) | |
} | |
} | |
} | |
} | |
fun sum(x: Int, y: Int): Int { | |
return x + y | |
} | |
fun main() { | |
GetRequest() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment