gradle kotlin DSL:
compile(group = "io.github.rybalkinsd", name = "kohttp", version = "0.4.0")
gradle groovy DSL:
compile 'io.github.rybalkinsd:kohttp:0.4.0'
maven:
<dependency>
<groupId>io.github.rybalkinsd</groupId>
<artifactId>kohttp</artifactId>
<version>0.4.0</version>
</dependency>
val response: okhttp3.Response = "https://google.com/search?q=iphone".httpGet()
// Response is `AutoClosable` access it with `use` to prevent resource leakage
reponse.use {
...
}
This extension starts a new coroutine with Unconfined dispatcher.
val response: Deferred<Response> = "https://google.com/search?q=iphone".asyncHttpGet()
val response: Response = httpGet {
host = "google.com"
path = "/search"
param {
"q" to "iphone"
"safe" to "off"
}
}
reponse.use {
...
}
val response: Response = httpGet {
host = "google.com"
path = "/search"
val variable = 123L
header {
"one" to 42
"two" to variable
"three" to json {
"a" to variable
"b" to json {
"b1" to "512"
}
"c" to listOf(1, 2.0, 3)
}
cookie {
"aaa" to "bbb"
"ccc" to 42
}
}
param {
"text" to "iphone"
"lr" to 213
}
}
reponse.use {
...
}
@Since 0.4.0
val response: Deferred<Response> = asyncHttpGet {
host = "google.com"
path = "/search"
header { ... }
param { ... }
}
form
body has a application/x-www-form-urlencoded
content type
val response: Response = httpPost {
host = "postman-echo.com"
path = "/post"
param { ... }
header {
...
cookie { ... }
}
body {
form { // Resulting form will not contain ' ', '\t', '\n'
"login" to "user" // login=user&
"email" to "[email protected]" // [email protected]
}
}
}
reponse.use {
...
}
json
body has a application/json
content type
val response: Response = httpPost {
host = "postman-echo.com"
path = "/post"
param { ... }
header {
...
cookie { ... }
}
body { // Resulting json will not contain ' ', '\t', '\n'
json { // {
"login" to "user" // "login": "user",
"email" to "[email protected]" // "email": "[email protected]"
} // }
}
}
reponse.use {
...
}
It is possible to customize CommonClientPool by setting up kohttp.yaml
in resource directory of your project.
You can check default values in com.kohttp.configuration.Config.kt
All time values are in Milliseconds
client:
connectTimeout: 5000
readTimeout: 10000
writeTimeout: 10000
followRedirects: true
followSslRedirects: true
connectionPool:
maxIdleConnections: 42
keepAliveDuration: 10000
TODO
TODO
@Since 0.3.0
Instead of .use { ... it.body?.string() ... }
it is now possible to read response body as string.
And also to map Headers
to listOf<Header>
to operate them easily.
val response: EagerResponse = "https://google.com/search?q=iphone".httpGet().eager()
// iterating over headers
response.headers.forEach { ... }
// manipulating body
response.body?.let { ... }
val response: EagerResponse = httpGet { }.eager()
// iterating over headers
response.headers.forEach { ... }
// manipulating body
response.body?.let { ... }