Home

Awesome

日本語

kHttpClient

Maven metadata URL

badge badge badge badge

This library is designed for making simple Http requests in Kotlin Multiplatform. It relies on Ktor Client and is implemented as a wrapper for it. Therefore, this library is available for use in Kotlin Multiplatform as long as the platform is supported by Ktor Client.

Engine Selection

Ktor Client provides several implementations of HttpClient engines for each platform. In this library, the following engines are selected for each platform. If you want to switch the engine, remove it from the dependencies and add another one. However, the behavior in that case cannot be guaranteed.

Usage

repositories {
    mavenCentral()
+   maven { url = uri("https://repo.repsy.io/mvn/uakihir0/public") }
}

dependencies {
+   implementation("work.socialhub:khttpclient:0.0.1-SNAPSHOT")
}

GET

To perform a GET request with query parameters, you can write it as follows:

 val response = HttpRequest()
    .url("https://httpbin.org/get")
    .query("key1", "value1")
    .query("key2", "value2")
    .get()

POST

To perform a POST request with form data and files, you can write it as follows:

val response = HttpRequest()
    .url("https://httpbin.org/post")
    .param("key", "value")
    .file("file", "test.txt", "content".toByteArray())
    .post()

To perform a POST request with a JSON string as the body, you can write it as follows:

val response = HttpRequest()
    .url("https://httpbin.org/post")
    .json("""{"key": "value"}""")
    .post()

For detailed usage, refer to the test code.

License

MIT License

Author

Akihiro Urushihara