Quellcode durchsuchen

Fix #365, cannot receive parameter from multipart form

ryoii vor 4 Jahren
Ursprung
Commit
34adaf2967

+ 5 - 3
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/adapter/http/router/dsl.kt

@@ -120,7 +120,7 @@ internal inline fun Route.httpAuthedMultiPart(
     path: String, crossinline body: Strategy2<HttpAuthedSession, List<PartData>>
 ) = routeWithHandle(path, HttpMethod.Post) {
     val parts = call.receiveMultipart().readAllParts()
-    val sessionKey = call.parameters["sessionKey"] ?: MahContext.SINGLE_SESSION_KEY
+    val sessionKey = parts.valueOrNull("sessionKey") ?: MahContext.SINGLE_SESSION_KEY
 
     this.body(getAuthedSession(sessionKey), parts)
 }
@@ -207,11 +207,13 @@ internal inline fun <reified R> PipelineContext<Unit, ApplicationCall>.paramOrNu
 /**
  * 接收 http multi part 值类型
  */
-internal fun List<PartData>.value(name: String) =
+internal fun List<PartData>.value(name: String) = valueOrNull(name) ?: throw IllegalParamException()
+
+internal fun List<PartData>.valueOrNull(name: String) =
     try {
         (filter { it.name == name }[0] as PartData.FormItem).value
     } catch (e: Exception) {
-        throw IllegalParamException()
+        null
     }
 
 /**

+ 1 - 1
mirai-api-http/src/test/kotlin/net/mamoe/mirai/api/http/adapter/launch/HttpAdapterLaunch.kt

@@ -7,7 +7,7 @@ import net.mamoe.mirai.api.http.adapter.MahAdapterFactory
 import net.mamoe.mirai.api.http.context.session.manager.DefaultSessionManager
 import net.mamoe.mirai.api.http.setting.MainSetting
 import net.mamoe.mirai.utils.BotConfiguration
-import org.junit.Test
+import kotlin.test.Test
 
 class HttpAdapterLaunch : LaunchTester() {