我在发送 http 请求时遇到问题,该请求的正文中的参数之一是文件。API 返回的响应不是我所期望的,所以我认为代码有问题。
suspend fun post(url: String, bodyParameters: MutableMap<String, Any>, headerParameters:
MutableMap<String, String>? = null): HttpResponse?{
val client = HttpClient()
try {
val call = client.call(url) {
method = HttpMethod.Post
body = MultiPartFormDataContent(
formData {
bodyParameters.forEach{(key, value) ->
when(value) {
is String -> {
append(key, value)
}
is File -> {
append(key, value.inputStream().asInput())
}
}
}
}
)
}
if(call.response.status.value == 500) {
return null
}
return call.response
Run Code Online (Sandbox Code Playgroud)
返回的响应状态是未发送文件时所期望的状态。关于为什么这行不通的任何想法?还有其他方法可以使用 Ktor 实现此目的吗?
| 归档时间: |
|
| 查看次数: |
3171 次 |
| 最近记录: |