Making POST, GET, PUT and DELETE Requests with HttpURLConnection using Kotlin

22 Jul 2023 Balmiki Mandal 0 Kotlin

How to make POST, GET, PUT and DELETE requests with HttpURLConnection using Kotlin

Making a request to a web server is an essential part of any modern app. Whether you are requesting information from an API or sending information via a form, knowing how to use HttpURLConnection with Kotlin is key. This article will show you how to make POST, GET, PUT, and DELETE requests using HttpURLConnection with Kotlin.

1. Setting up HttpURLConnection

First, we need to set up a connection. This will be done using the HttpURLConnection class. We’ll start by creating a URL object for our endpoint and then instantiate an HttpURLConnection object from it:

val url = URL("http://www.example.com") 
val connection = url.openConnection() as HttpURLConnection

2. Making a POST Request

Now that we have our connection set up, we can make a POST request. To do this, we need to set the request method to “POST” and the Content-Type header to “application/x-www-form-urlencoded”:

connection.requestMethod = "POST" 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded")

We also need to set the DoOutput parameter of the connection to true so that we can write data to the request body. Once this is done, we can write the data we want to post to the request body:

connection.doOutput = true 
val postData = "name=John&age=24" 
val outputStreamWriter = OutputStreamWriter(connection.outputStream) 
outputStreamWriter.write(postData) 
outputStreamWriter.flush()

Finally, we can send the request by calling the connect() method on the connection and read the response by reading from the input stream. We can do this with the following code:

connection.connect() 
val inputStreamReader = BufferedReader(InputStreamReader(connection.inputStream)) 
val response = StringBuilder() 
var line: String? 
while (line != null) { 
    line = inputStreamReader.readLine() 
    if (line != null) { 
        response.append(line) 
    } 
} 

println("Response: $response")

3. Making a GET Request

Making a GET request with HttpURLConnection is simpler than making a POST request. All we need to do is set the request method to “GET” and then call the connect() method on the connection. We can read the response by reading from the input stream as before.

connection.requestMethod = "GET" 
connection.connect() 
val inputStreamReader = BufferedReader(InputStreamReader(connection.inputStream)) 
val response = StringBuilder() 
var line: String? 
while (line != null) { 
    line = inputStreamReader.readLine() 
    if (line != null) { 
        response.append(line) 
    } 
} 

println("Response: $response")

4. Making a PUT Request

Making a PUT request with HttpURLConnection is similar to making a POST request. The only difference is that we need to set the request method to “PUT” instead of “POST”:

connection.requestMethod = "PUT" 
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded") 
connection.doOutput = true 
val putData = "name=John&age=24" 
val outputStreamWriter = OutputStreamWriter(connection.outputStream) 
outputStreamWriter.write(putData) 
outputStreamWriter.flush() 
connection.connect()

Again, we can read the response by reading from the input stream. The rest of the code is the same as in the POST request example.

5. Making a DELETE Request

Making a DELETE request with HttpURLConnection is similar to making a GET request. All we need to do is set the request method to “DELETE” and then call the connect() method on the connection. We can read the response by reading from the input stream as before.

connection.requestMethod = "DELETE" 
connection.connect() 
val inputStreamReader = BufferedReader(InputStreamReader(connection.inputStream)) 
val response = StringBuilder() 
var line: String? 
while (line != null) { 
    line = inputStreamReader.readLine() 
    if (line != null) { 
        response.append(line) 
    } 
} 

println("Response: $response")

And that’s it! You now know how to make POST, GET, PUT, and DELETE requests with HttpURLConnection using Kotlin

 

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.