[Ktor] 2. Request
Ktor Client ๊ฐ์ด๋ ์์ฝ ์ ๋ฆฌ
- Ktor Documents
- ๊ธฐ์ค ๋ฒ์ :
1.6.2
- ์๋ฆฌ์ฆ
HttpClient
์ request
ํจ์๋ก HTTP Request๋ฅผ ์์ฑํ ์ ์๋ค.
request
ํจ์ ์๊ทธ๋์ฒ - ๋ํ์ ์ธ ์์
inline suspend fun <T> HttpClient.request(urlString: String, block: HttpRequestBuilder.() -> Unit = {}): T
inline suspend fun <T> HttpClient.request(builder: HttpRequestBuilder = HttpRequestBuilder()): T
- ์ด์ธ ๋ชจ๋ ํจ์ ์๊ทธ๋์ฒ๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์๋ค.
HttpRequestBuilder
๋ฅผ ํตํด HTTP method, ํค๋, ์ฟ ํค, ๋ฐ๋ ๋ฑ์ ์ค์ ํ ์ ์๋ค.
val response: HttpResponse = client.request("https://ktor.io/") {
// Configure request parameters exposed by HttpRequestBuilder
}
Request ํ๋ผ๋ฏธํฐ
> HTTP method
request
ํจ์๋ฅผ ํธ์ถํ ๋method
ํ๋กํผํฐ๋ฅผ ํตํด HTTP method๋ฅผ ์ง์ ํ ์ ์๋ค.val response: HttpResponse = client.request("https://ktor.io/") { method = HttpMethod.Get }
request
ํจ์ ์ธ์HttpClient
๋ ๊ฐ method ๋ณ ๊ธฐ๋ณธ ํจ์๋ฅผ ์ ๊ณตํ๋ค. - get, post, put ๋ฑval response: HttpResponse = client.get("https://ktor.io/")
> ํค๋
headers
ํจ์๋ฅผ ์ด์ฉํด Request์ ํค๋๋ฅผ ์ถ๊ฐํ ์ ์๋ค.
fun HttpRequestBuilder.headers(block: HeadersBuilder.() -> Unit): HeadersBuilder
val response: HttpResponse = client.get("https://ktor.io/") {
headers {
append(HttpHeaders.Accept, "text/html")
append(HttpHeaders.Authorization, "token")
append(HttpHeaders.UserAgent, "ktor client")
}
}
> ์ฟ ํค
์ฟ ํค ์ ์ก์ cookie
ํจ์๋ฅผ ์ด์ฉํ๋ค.
fun HttpRequestBuilder.cookie(
name: String,
value: String,
maxAge: Int = 0,
expires: GMTDate? = null,
domain: String? = null,
path: String? = null,
secure: Boolean = false,
httpOnly: Boolean = false,
extensions: Map<String, String?> = emptyMap()
)
val response: HttpResponse = client.get("https://ktor.io/") {
cookie(name = "user_name", value = "jetbrains", expires = GMTDate(
seconds = 0,
minutes = 0,
hours = 10,
dayOfMonth = 1,
month = Month.APRIL,
year = 2022
))
}
*ํธ์ถ ๊ฐ ์ฟ ํค๋ฅผ ์ ์งํด์ฃผ๋ Cookiesํ๋ฌ๊ทธ์ธ๋ ์ ๊ณตํ๋ค.
> Query parameters
parameter
ํจ์๋ฅผ ์ด์ฉํ์ฌ ์ถ๊ฐํ๋ค.
fun HttpRequestBuilder.parameter(key: String, value: Any?)
val response: HttpResponse = client.get("http://localhost:8080/products") {
parameter("price", "asc")
}
Request body
Request body๋ฅผ ์ถ๊ฐํ๋ ค๋ฉด HttpRequestBuilder
์
body
ํ๋กํผํฐ๋ฅผ ์ด์ฉํด์ผ ํ๋ค. body
ํ๋กํผํฐ๋ ํ
์คํธ / ํด๋์ค ์ธ์คํด์ค / form data ๋ฑ ์ฌ๋ฌ ํ์
์ ์ง์ํ๋ค. (ํ์
์ด Any
์ธ ๊ฒ์ผ๋ก ์ ์ ์๋ค.)
var body: Any
์์) ํ ์คํธ
val response: HttpResponse = client.post("http://localhost:8080/post") {
body = "Body content"
}
์์) ๊ฐ์ฒด
Json ํ๋ฌ๊ทธ์ธ์ ์ฌ์ฉํ๋ฉด ํด๋์ค ์ธ์คํด์ค๋ฅผ Json ํ์์ผ๋ก body์ ๋ฃ์ ์ ์๋ค. body
ํ๋กํผํฐ์ ํด๋์ค ์ธ์คํด์ค๋ฅผ ๋ฃ๊ณ , content type์ application.json
์ผ๋ก ์ค์ ํ๋ฉด ๋๋ค.
client.post<Unit>("http://localhost:8080/post") {
contentType(ContentType.Application.Json)
body = Customer("Jet", "Brains")
}
์์) Form data
submitForm
ํจ์๋ฅผ ์ด์ฉํ์ฌ x-www-form-urlencoded
๊ณผ multipart/form-data
ํ์
์ form data ์ ์ก์ด ๊ฐ๋ฅํ๋ค.
val response: HttpResponse = client.submitForm(
url = "http://localhost:8080/get",
formParameters = Parameters.build {
append("first_name", "Jet")
append("last_name", "Brains")
},
encodeInQuery = true
)
์์) ํ์ผ ์ ๋ก๋
submitFormWithBinaryData
ํจ์๋ฅผ ์ด์ฉํ์ฌ form data์ ํ์ผ์ ํฌํจ์ํจ๋ค. (์คํ ๊ฐ๋ฅํ ์ฝ๋ ์์ ๋ ์ฌ๊ธฐ์์ ํ์ธํ ์ ์๋ค.)
val response: HttpResponse = client.submitFormWithBinaryData(
url = "http://localhost:8080/upload",
formData = formData {
append("description", "Ktor logo")
append("image", File("ktor_logo.png").readBytes(), Headers.build {
append(HttpHeaders.ContentType, "image/png")
append(HttpHeaders.ContentDisposition, "filename=ktor_logo.png")
})
}
) {
// onUpload ํ์ฅํจ์๋ฅผ ์ด์ฉํ๋ฉด ์
๋ก๋ progress ํ์๋ ๊ฐ๋ฅํ๋ค.
onUpload { bytesSentTotal, contentLength ->
println("Sent $bytesSentTotal bytes from $contentLength")
}
}
๋ณ๋ ฌ ์์ฒญ
๋ ๊ฐ์ ์์ฒญ์ ํ ๋ฒ์ ๋ณด๋ด๋ ๊ฒฝ์ฐ, HttpClient๋ ์ฒซ ๋ฒ์งธ ์์ฒญ์ด ๋๋ ๋๊น์ง ๋ ๋ฒ์งธ ์์ฒญ์ ๋๊ธฐ์ํจ๋ค. ๋ง์ฝ ์ฌ๋ฌ ์์ฒญ์ ํ ๋ฒ์ ๋ณด๋ด๊ณ ์ถ๋ค๋ฉด, launch
๋๋ async
ํจ์๋ฅผ ์ด์ฉํ๋ค.
์๋ ์ฝ๋๋ ๋ ์์ฒญ์ ๋น๋๊ธฐ์ ์ผ๋ก ์คํํ๋ค (์ ์ฒด ์ํ ์ฐธ๊ณ ๋งํฌ):
val client = HttpClient(CIO)
val firstRequest: Deferred<String> = async { client.get("http://localhost:8080/path1") }
val secondRequest: Deferred<String> = async { client.get("http://localhost:8080/path2") }
val firstRequestContent = firstRequest.await()
val secondRequestContent = secondRequest.await()
์์ฒญ ์ทจ์
launch
ํจ์๊ฐ ๋ฐํํ Job
์ ์ด์ฉํ์ฌ ์ทจ์ํ๋ค.
val client = HttpClient(CIO)
val job = launch {
val requestContent: String = client.get("http://localhost:8080")
}
job.cancel()
Comments