通过 ktor-client 发布多部分
我必须通过 ktor-client 发出多部分请求。请求方法:
suspend fun uploadPhoto(userId: String, bitmapPhoto: BitmapPhoto) {
kotlin.runCatching {
val response: HttpResponse = httpClient.submitFormWithBinaryData(
url = BASE_URL.plus("$userId/${bitmapPhoto.name}"),
formData = formData {
append("file", bitmapPhoto.bitmap.toByteArray(),
Headers.build {
append(HttpHeaders.ContentType, "multipart/form-data; boundary=boundary")
append(HttpHeaders.ContentDisposition, "form-data; name=file; filename=${bitmapPhoto.name}")
}
)
}
)
}
执行请求时服务器返回错误。 有正确请求的示例
POST https://host-name.com
Content-Type: multipart/form-data; boundary=boundary;
--boundary
Content-Disposition: form-data; name="file"; filename="image.png"
< C:/path/image.png
--boundary
所以我不明白我的kotlin方法在哪里不正确
更新 方法的第一个变体是
suspend fun uploadPhoto(userId: String, bitmapPhoto: BitmapPhoto) {
kotlin.runCatching {
val response: HttpResponse = httpClient.submitFormWithBinaryData(
url = BASE_URL.plus("$userId/${bitmapPhoto.name}"),
formData = formData {
append("file", bitmapPhoto.bitmap.toByteArray(),
Headers.build {
append(HttpHeaders.ContentType, "image/png")
append(HttpHeaders.ContentDisposition, "filename=${bitmapPhoto.name}")
}
)
}
)
}
但是它不起作用所以
I have to make a multipart request via ktor-client. Request method:
suspend fun uploadPhoto(userId: String, bitmapPhoto: BitmapPhoto) {
kotlin.runCatching {
val response: HttpResponse = httpClient.submitFormWithBinaryData(
url = BASE_URL.plus("$userId/${bitmapPhoto.name}"),
formData = formData {
append("file", bitmapPhoto.bitmap.toByteArray(),
Headers.build {
append(HttpHeaders.ContentType, "multipart/form-data; boundary=boundary")
append(HttpHeaders.ContentDisposition, "form-data; name=file; filename=${bitmapPhoto.name}")
}
)
}
)
}
When executing request the server returns an error.
There is the example of correct request
POST https://host-name.com
Content-Type: multipart/form-data; boundary=boundary;
--boundary
Content-Disposition: form-data; name="file"; filename="image.png"
< C:/path/image.png
--boundary
So i don't understand where is my kotlin method incorrect
UPDATE
The 1st variant of method was
suspend fun uploadPhoto(userId: String, bitmapPhoto: BitmapPhoto) {
kotlin.runCatching {
val response: HttpResponse = httpClient.submitFormWithBinaryData(
url = BASE_URL.plus("$userId/${bitmapPhoto.name}"),
formData = formData {
append("file", bitmapPhoto.bitmap.toByteArray(),
Headers.build {
append(HttpHeaders.ContentType, "image/png")
append(HttpHeaders.ContentDisposition, "filename=${bitmapPhoto.name}")
}
)
}
)
}
But it wasn't work so
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是在以下标题中:
content-type
标题应具有文件内容的MIME类型(bitmapphoto
),而不是请求正义的类型( KTOR自动添加它),例如image/png
。content-disposition
标题应仅具有fileName = $ {bitmapphoto.name}
值的值,因为KTOR添加了form> form> form> form-data;名称=文件;
自动。因此,您的代码应该看起来像这样:有关更多信息,请参阅 ktor文档 。
The problem is in the following headers:
The
Content-Type
header should have a MIME type of the contents of your file (bitmapPhoto
), not a type of the request body (Ktor adds it automatically), e.g.image/png
. TheContent-Disposition
header should have onlyfilename=${bitmapPhoto.name}
value for your case because Ktor addsform-data; name=file;
automatically. So your code should look like this:For more information, please refer to the Ktor documentation.