How is curl's request expressed in HttpURLConnection?

Asked 1 years ago, Updated 1 years ago, 86 views

I am writing the code on Kotlin on Android.
I'd like to get my github username from the access token.

 curl https://api.github.com/user?--user username: [token]

With curl, you can get the information you want to get with the above expression.How should I write this request in kotlin?I wrote a method for obtaining a username using an access token as an argument below, but I cannot submit a request.Perhaps it is because you put the curl command --user in the URL description.How do you describe the command part of curl as HttpURLConnection?I would appreciate it if you could tell me one of them.

private fun GetUserName(token:String):String{
    var userName=""
    try{
        val url=URL("https://api.github.com/user?--user username:$token")
        // Creating HttpURLConnection Objects for Connections
        var connection —HttpURLConnection=url.openConnection() as HttpURLConnection
        try{
            // Set the connection timeout.
            connection.connectTimeout=100000
            // Set the response data read timeout.
            connection.readTimeout=100000
            // Configure Accept-Language in the header.
            connection.setRequestProperty("Accept-Language", Locale.getDefault().toString())
            // Set Content-Type in Header
            connection.addRequestProperty("Content-Type", "application/json; charset=UTF-8")
            // Configuring Request Methods
            connection.requestMethod="GET"
            // Configuring Not Automatically Allow Redirects
            connection.instanceFollowRedirects=false
            // True if reading data from URL connection
            connection.doInput=true
            // True to write data to URL connection
            connection.doOutput=false
            // CONNECTION
            connection.connect()
            // Obtaining Response Codes
            valcode = connection.responseCode
            // Log.d("Response code is ", code);

            if(code==204){
                Log.d (TAG, "Receive Success")
            }

            // send a response from the server to the standard output
            valreader=BufferedReader(InputStreamReader(connection.getInputStream()))
            var xml=""
            varline —String

            while(reader.readLine()!=null){
                line=reader.readLine()
                xml+=line
                Log.d (TAG, line)
            }

            Log.d(TAG, "Retrieved Information List $xml")
            reader.close()

android java http kotlin curl

2022-09-30 13:53

1 Answers

Add the Authorization header.

I think you can add the following.

connection.setRequestProperty("Authorization", "token" + token)

Then, the next line is

val url=URL("https://api.github.com/user?--user username:$token")

The constructor argument in java.net.URL is a string representing the URL, so you cannot add cURL options.Also, ? is not required.The following is sufficient:

val url=URL ("https://api.github.com/user")

See Javadoc for java.net.URL.
https://docs.oracle.com/javase/8/docs/api/java/net/URL.html

See below for the GitHub API.
https://developer.github.com/apps/building-oauth-apps/authorizing-oauth-apps/


2022-09-30 13:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.