https://kotlinlang.org logo
#announcements
Title
# announcements
i

Ifvwm

11/10/2020, 2:26 AM
I'm trying to change a simple python to kotlin, but I don't know where is wrong, https://paste.ubuntu.com/p/zp4x9yz36X/
it's just simple post requests, but in the function step2(), it just doesn't work, I don't know why
but python and curl worked, something is wrong with httpurlconnection?
n

nanodeath

11/10/2020, 2:29 AM
recommend using an http library here, but I'm trying to see what's going wrong here. maybe you need to read the response and close the connection.
i

Ifvwm

11/10/2020, 2:31 AM
which http library you suggest? the official doc is here https://docs.confluent.io/5.5.2/kafka-rest/quickstart.html
g

gildor

11/10/2020, 2:33 AM
Probably use ktor-client is the easiest way, it has support of json parsing too
HttpUrlConnection also works, but it has very clunky interface and a lot of boilerplate
Also looks that you really don't need all those try/catch which rethrow exceptions
There are many Kotlin stdlib functions which can improve this code too
What is exactly doesn't work on step2?
i

Ifvwm

11/10/2020, 2:37 AM
step2 is subscription
curl -X POST -H "Content-Type: application/vnd.kafka.v2+json" --data '{"name": "my_consumer_instance", "format": "json", "auto.offset.reset": "earliest"}' http://49.5.6.84:8082/consumers/testName
curl -X POST -H "Content-Type: application/vnd.kafka.v2+json" --data '{"topics":["jsontest"]}' http://49.5.6.84:8082/consumers/testName/instances/my_consumer_instance/subscription
curl -X GET -H "Accept: application/vnd.kafka.json.v2+json" http://49.5.6.84:8082/consumers/testName/instances/my_consumer_instance/records
those three curl is step1 step2 and step3
step1 create an instace for consumer, step2 to subscript a topic, step3 to get messages from that topic, python code and curl worked well
but with kotlin, the step2 doesn't work, there's no response in step2, see https://docs.confluent.io/5.5.2/kafka-rest/quickstart.html
I also tried OkHttp, same as httpurlconnection
g

gildor

11/10/2020, 2:43 AM
What do you mean no response? I don't see that you read response on step 2
Also in docs there is no response
i

Ifvwm

11/10/2020, 2:48 AM
aha, after I add read in step2, it worked!
I add this
Copy code
try {
        val reader: BufferedReader = BufferedReader(InputStreamReader(httpconn2.inputStream))
        val response = reader.readLine()
        println(response)

    } catch (exception: Exception) {
        throw Exception("Exception while push the notification  $exception.message")
    }
step2 worked! and step3 can get messgaes now
g

gildor

11/10/2020, 3:18 AM
I think your issue not even lack of reading, but because you do not close output stream
i

Ifvwm

11/10/2020, 3:20 AM
no, I tried to close the output stream, it still be error 500
g

gildor

11/10/2020, 3:23 AM
So, your “not working” was 500 error? 500 means server error, looks like a bug of server It still related to what kind api you use, using raw httpurlconnection or even Okhttp is kinda low level, and you have to handle all possible edge cases yourself, now your code is leaking connection
You should use
use
blocks to handle resources
Also there are many helper extensions which allow you to read input/output strwams
i

Ifvwm

11/10/2020, 3:42 AM
ok, I would try ktor-client, any other lib suggested?
a

Animesh Sahu

11/10/2020, 4:08 AM
You can have a look at Fuel if you just want to parse the response (as a Client)
g

gildor

11/10/2020, 4:09 AM
I would recommend Ktor/Retrofit.
2 Views