fix: rewrite SageClient and BricksHttp to use URL-encoded string body instead of FormDataContent (not available in Ktor common)
This commit is contained in:
parent
e4f35ec39c
commit
b342ae85c6
@ -5,7 +5,6 @@ import io.ktor.client.engine.cio.*
|
|||||||
import io.ktor.client.plugins.cookies.*
|
import io.ktor.client.plugins.cookies.*
|
||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
import io.ktor.client.statement.*
|
import io.ktor.client.statement.*
|
||||||
import io.ktor.http.content.FormDataContent
|
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.*
|
||||||
|
|
||||||
@ -83,8 +82,12 @@ class BricksHttp(private val context: BricksContext? = null) {
|
|||||||
form: Parameters,
|
form: Parameters,
|
||||||
authToken: String = ""
|
authToken: String = ""
|
||||||
): String {
|
): String {
|
||||||
|
val formBody = form.flattenEntries().joinToString("&") { (k, v) ->
|
||||||
|
"${encodeURLParameter(k)}=${encodeURLParameter(v)}"
|
||||||
|
}
|
||||||
val response = client.post(url) {
|
val response = client.post(url) {
|
||||||
setBody(FormDataContent(form))
|
contentType(ContentType.Application.FormUrlEncoded)
|
||||||
|
setBody(formBody)
|
||||||
if (authToken.isNotEmpty()) {
|
if (authToken.isNotEmpty()) {
|
||||||
header(HttpHeaders.Authorization, "Bearer $authToken")
|
header(HttpHeaders.Authorization, "Bearer $authToken")
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,9 +7,7 @@ import io.ktor.client.engine.cio.*
|
|||||||
import io.ktor.client.plugins.cookies.*
|
import io.ktor.client.plugins.cookies.*
|
||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
import io.ktor.client.statement.*
|
import io.ktor.client.statement.*
|
||||||
import io.ktor.http.content.FormDataContent
|
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import kotlinx.coroutines.flow.asStateFlow
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import kotlinx.coroutines.sync.withLock
|
import kotlinx.coroutines.sync.withLock
|
||||||
import kotlinx.serialization.json.*
|
import kotlinx.serialization.json.*
|
||||||
@ -57,13 +55,11 @@ class SageClient {
|
|||||||
_loginError.value = null
|
_loginError.value = null
|
||||||
try {
|
try {
|
||||||
val url = "$baseUrl/rbac/user/userpassword_login.dspy"
|
val url = "$baseUrl/rbac/user/userpassword_login.dspy"
|
||||||
val formParameters = Parameters.build {
|
val formBody = "username=${encodeURLParameter(username)}&passwd=${encodeURLParameter(password)}"
|
||||||
append("username", username)
|
|
||||||
append("passwd", password)
|
|
||||||
}
|
|
||||||
|
|
||||||
val response = client.post(url) {
|
val response = client.post(url) {
|
||||||
setBody(FormDataContent(formParameters))
|
contentType(ContentType.Application.FormUrlEncoded)
|
||||||
|
setBody(formBody)
|
||||||
}
|
}
|
||||||
|
|
||||||
val body = response.bodyAsText()
|
val body = response.bodyAsText()
|
||||||
@ -142,7 +138,7 @@ class SageClient {
|
|||||||
path: String,
|
path: String,
|
||||||
params: Map<String, String> = emptyMap(),
|
params: Map<String, String> = emptyMap(),
|
||||||
method: String = "GET",
|
method: String = "GET",
|
||||||
formBody: Parameters? = null,
|
formBody: String = "",
|
||||||
jsonBody: JsonObject? = null
|
jsonBody: JsonObject? = null
|
||||||
): Result<JsonObject> = mutex.withLock {
|
): Result<JsonObject> = mutex.withLock {
|
||||||
try {
|
try {
|
||||||
@ -158,7 +154,7 @@ class SageClient {
|
|||||||
contentType(ContentType.Application.Json)
|
contentType(ContentType.Application.Json)
|
||||||
setBody(jsonBody)
|
setBody(jsonBody)
|
||||||
}
|
}
|
||||||
formBody != null -> {
|
formBody.isNotEmpty() -> {
|
||||||
contentType(ContentType.Application.FormUrlEncoded)
|
contentType(ContentType.Application.FormUrlEncoded)
|
||||||
setBody(formBody)
|
setBody(formBody)
|
||||||
}
|
}
|
||||||
@ -197,8 +193,6 @@ class SageClient {
|
|||||||
*/
|
*/
|
||||||
suspend fun logout() = mutex.withLock {
|
suspend fun logout() = mutex.withLock {
|
||||||
try {
|
try {
|
||||||
// 清除 cookies
|
|
||||||
cookieStorage.get(URLBuilder(baseUrl).build())
|
|
||||||
_isLoggedIn.value = false
|
_isLoggedIn.value = false
|
||||||
println("[Sage] Logged out")
|
println("[Sage] Logged out")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user