【发布时间】:2021-10-21 22:25:47
【问题描述】:
有谁知道如何编写自定义Multipart.FormData 转换器的覆盖函数convertForReceive?
我想使用转换器将多部分请求转换为我的类,但我不知道它是如何工作的。
我有:
Application.kt
install(ContentNegotiation) {
json()
register(ContentType.MultiPart.FormData, CustomMultipartConverter)
}
CustomMultipartConverter
object CustomMultipartConverter: ContentConverter {
override suspend fun convertForReceive(context: PipelineContext<ApplicationReceiveRequest, ApplicationCall>): Any? {
TODO("Not yet implemented")
}
override suspend fun convertForSend(
context: PipelineContext<Any, ApplicationCall>,
contentType: ContentType,
value: Any
): Any? {
TODO("Not yet implemented")
}
}
请求课程
class CreatePostRequest(
val text: String,
val image: File? = null
)
路线
route("v1/posts") {
authenticate {
route("create") {
val authJWT = call.authentication.principal as JWTAtuh
val request = call.receive<CreatePostRequest>()
//myCode
call.respond(HttpStatusCode.OK)
}
}
}
【问题讨论】:
标签: kotlin multipartform-data ktor