MOCKSTACKS
EN
Questions And Answers

More Tutorials








Kotlin HTTP GET request

An HTTP request is made by a client, to a named host, which is located on a server. The aim of the request is to access a resource on the server.

Example of Kotlin HTTP Request

package com.zetcode

import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse

fun main() {
    val client = HttpClient.newBuilder().build();
    val request = HttpRequest.newBuilder()
        .uri(URI.create("https://jsonplaceholder.typicode.com/posts"))
        .build();
        
    val response = client.send(request, HttpResponse.BodyHandlers.ofString());
    println(response.body())
}


We create a client GET request to the webcode.me webpage using below syntax:

val client = HttpClient.newBuilder().build();


After the client is created Successfully, we create the URI to end point using below syntax:

    
val request = HttpRequest.newBuilder()
        .uri(URI.create("https://jsonplaceholder.typicode.com/posts"))
        .build();


After building the URI we call the HTTP request to retrieve the response using below syntax:

val response = client.send(request, HttpResponse.BodyHandlers.ofString());

Conclusion

In this page (written and validated by ) you learned about Kotlin HTTP GET request . What's Next? If you are interested in completing Kotlin tutorial, we encourage you simply to start here: Kotlin Tutorial.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.