为什么要与“图像”/*&quot倍增改造。得到不好的要求?

发布于 2025-02-13 03:29:05 字数 1346 浏览 0 评论 0原文

我想发布一个带有一些细节的产品,例如名称,desc等。以及图像。但是我得到了400个不好的要求。

val namebody = name.toRequestBody("text/plain".toMediaType())
val priceBody = price.toRequestBody("text/plain".toMediaType())
val descriptionBody = description.toRequestBody("text/plain".toMediaType())
val categoryBody = category.toRequestBody("text/plain".toMediaType())
val locationBody = location.toRequestBody("text/plain".toMediaType())

val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
val image = MultipartBody.Part.createFormData("image", imageFile.name, imageBody)
@Multipart
    @POST("seller/product")
    suspend fun postProductSeller(
        @Header("access_token") access_token: String,
        @Part("name") name: RequestBody,
        @Part("description") description: RequestBody,
        @Part("base_price") base_price: RequestBody,
        @Part("category_ids") category_ids: RequestBody,
        @Part("location") location: RequestBody,
        @Part image: MultipartBody.Part
    )

因此,经过多次尝试,我很成功地将图像进行改造,但我必须将此行更改

val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())

为Spesific Image格式,示例JPG,PNG。

val imageBody = imageFile.asRequestBody("image/jpg".toMediaTypeOrNull())

有人知道为什么会发生这种情况吗?如果要上传任何形式的图像文件,该怎么办?谢谢

I want to post a Product with some details like name, desc, etc. and image too. But I got 400 bad request.

val namebody = name.toRequestBody("text/plain".toMediaType())
val priceBody = price.toRequestBody("text/plain".toMediaType())
val descriptionBody = description.toRequestBody("text/plain".toMediaType())
val categoryBody = category.toRequestBody("text/plain".toMediaType())
val locationBody = location.toRequestBody("text/plain".toMediaType())

val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())
val image = MultipartBody.Part.createFormData("image", imageFile.name, imageBody)
@Multipart
    @POST("seller/product")
    suspend fun postProductSeller(
        @Header("access_token") access_token: String,
        @Part("name") name: RequestBody,
        @Part("description") description: RequestBody,
        @Part("base_price") base_price: RequestBody,
        @Part("category_ids") category_ids: RequestBody,
        @Part("location") location: RequestBody,
        @Part image: MultipartBody.Part
    )

So after many tries, I have success to post image with retrofit but I have to change this line

val imageBody = imageFile.asRequestBody("image/*".toMediaTypeOrNull())

to a spesific image format, example jpg, png.

val imageBody = imageFile.asRequestBody("image/jpg".toMediaTypeOrNull())

And anyone knows why is this happen? And how should I do if I want to upload any kind of image files? Thank you

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文