I have a simple POST request:
val url = URL(SEND_URL)
val connection = url.openConnection() as HttpURLConnection
connection.doOutput = true
connection.addRequestProperty("Authorization", "...")
connection.addRequestProperty("Content-Type", "application/json")
val messageJson = Gson().toJson(message)
val outputStream = OutputStreamWriter(connection.outputStream, "UTF-8")
outputStream.write(messageJson)
outputStream.flush()
outputStream.close()
connection.connect()
It does not send anything.
When I add (instead of connection.connect())
connection.responseCode
connection.disconnect()
It works.
Could someone please explain to me why is it so? I do not need to read the response or responseCode but without it, my request is not being sent.