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.
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.
- Apple: Darwin
- JVM: OkHttp
- Windows: WinHttp
- Linux: Curl
In a Linux environment, WebSocket cannot be used due to the specifications of the Engine.
If you want to use WebSocket, please change the dependencies by removing curl and adding cio.
Note that in this case, some servers may be inaccessible due to partial lack of TLS support.
For more details about the engine, please refer to the official Ktor documentation.
repositories {
mavenCentral()
}
dependencies {
+ implementation("work.socialhub:khttpclient:0.0.8")
}repositories {
mavenCentral()
+ maven { url = uri("https://repo.repsy.io/mvn/uakihir0/public") }
}
dependencies {
+ implementation("work.socialhub:khttpclient:0.0.9-SNAPSHOT")
}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()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.
MIT License