如果我使用submitFormWithBinaryData(),如何向 ktor 请求添加标头?

发布于 2025-01-09 05:42:50 字数 497 浏览 3 评论 0原文

下面的代码是使用 ktor 和 kmm 上传文件......

val client = HttpClient(Apache) {}
    val file = File("path/to/some.file")
    val chatId = "123"
    
    client.submitFormWithBinaryData(
        url = "https://api.telegram.org/bot<token>/sendDocument?chat_id=$chatId",
        formData = formData {
            append("document", file.readBytes(), Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${file.name}")
            })
        }
    )

Code below is to upload an file using ktor and kmm ...

val client = HttpClient(Apache) {}
    val file = File("path/to/some.file")
    val chatId = "123"
    
    client.submitFormWithBinaryData(
        url = "https://api.telegram.org/bot<token>/sendDocument?chat_id=$chatId",
        formData = formData {
            append("document", file.readBytes(), Headers.build {
                append(HttpHeaders.ContentDisposition, "filename=${file.name}")
            })
        }
    )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

带上头具痛哭 2025-01-16 05:42:50

您无法使用 submitFormWithBinaryData 方法来执行此操作。使用 post请求方法。这是一个例子:

client.post("https://api.telegram.org/bot<token>/sendDocument?chat_id=$chatId") {
    header("custom", "value")
    body = MultiPartFormDataContent(formData {
        append("document", file.readBytes(), Headers.build {
            append(HttpHeaders.ContentDisposition, "filename=${file.name}")
        })
    })
}

You can't do that using the submitFormWithBinaryData method. Use the post or request method. Here is an example:

client.post("https://api.telegram.org/bot<token>/sendDocument?chat_id=$chatId") {
    header("custom", "value")
    body = MultiPartFormDataContent(formData {
        append("document", file.readBytes(), Headers.build {
            append(HttpHeaders.ContentDisposition, "filename=${file.name}")
        })
    })
}
苍风燃霜 2025-01-16 05:42:50

这是我的代码...

     val response = client.post<String>("${PublicData.BASEURL}"+"classes/UserFiles"){
                headers {
                    append("X-Parse-Application-Id", PublicData.Application_Id )
                    append("X-Parse-REST-API-Key", PublicData.REST_API_Key)
                    append("Content-Type", "application/json")
                }
    
                contentType(ContentType.Application.Json)
                body =  MultiPartFormDataContent(formData {
                    headersOf("X-Parse-Application-Id", PublicData.Application_Id)
                    headersOf("X-Parse-REST-API-Key", PublicData.REST_API_Key)
                    append("file_type", "Ktor klogo")
                    append("encryption_tool_id", "Ktorkk logo")
                    append("user_id", "Ktor kklogo")
                    append("query", "Ktor kklogo")
                    append("file", file, Headers.build {
                        append(HttpHeaders.ContentType, ContentType.Application.Json)
                        append(HttpHeaders.ContentDisposition, "filename=asd")
                    })
                })
    
            }

this is my code ...

     val response = client.post<String>("${PublicData.BASEURL}"+"classes/UserFiles"){
                headers {
                    append("X-Parse-Application-Id", PublicData.Application_Id )
                    append("X-Parse-REST-API-Key", PublicData.REST_API_Key)
                    append("Content-Type", "application/json")
                }
    
                contentType(ContentType.Application.Json)
                body =  MultiPartFormDataContent(formData {
                    headersOf("X-Parse-Application-Id", PublicData.Application_Id)
                    headersOf("X-Parse-REST-API-Key", PublicData.REST_API_Key)
                    append("file_type", "Ktor klogo")
                    append("encryption_tool_id", "Ktorkk logo")
                    append("user_id", "Ktor kklogo")
                    append("query", "Ktor kklogo")
                    append("file", file, Headers.build {
                        append(HttpHeaders.ContentType, ContentType.Application.Json)
                        append(HttpHeaders.ContentDisposition, "filename=asd")
                    })
                })
    
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文