通过 ktor-client 发布多部分

发布于 2025-01-19 19:41:08 字数 1768 浏览 1 评论 0原文

我必须通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2025-01-26 19:41:08

问题是在以下标题中:

append(HttpHeaders.ContentType, "multipart/form-data; boundary=boundary")
append(HttpHeaders.ContentDisposition, "form-data; name=file; filename=${bitmapPhoto.name}")

content-type标题应具有文件内容的MIME类型(bitmapphoto),而不是请求正义的类型( KTOR自动添加它),例如image/pngcontent-disposition标题应仅具有fileName = $ {bitmapphoto.name}值的值,因为KTOR添加了form> form> form> form-data;名称=文件;自动。因此,您的代码应该看起来像这样:

append(HttpHeaders.ContentType, "image/png")
append(HttpHeaders.ContentDisposition, "filename=${bitmapPhoto.name}")

有关更多信息,请参阅 ktor文档

The problem is in the following headers:

append(HttpHeaders.ContentType, "multipart/form-data; boundary=boundary")
append(HttpHeaders.ContentDisposition, "form-data; name=file; filename=${bitmapPhoto.name}")

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. The Content-Disposition header should have only filename=${bitmapPhoto.name} value for your case because Ktor adds form-data; name=file; automatically. So your code should look like this:

append(HttpHeaders.ContentType, "image/png")
append(HttpHeaders.ContentDisposition, "filename=${bitmapPhoto.name}")

For more information, please refer to the Ktor documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文