2.0.0-β1:利用改造发送多部分与文件部分、文件

2023-09-06 03:32:30 作者:夜、如此安静

我有问题,发送multipartRequest服务器。

I have problem with sending multipartRequest to server.

RequestBody file = RequestBody.create(MediaType.parse("application/json"), myFile);
        return apiService.updateProfile2(token, file);


@Multipart
@POST("/profile/update")
Call<RegistrationResponse> updateProfile2(@Header(value = "X-AUTH-TOKEN") String toke, @Part(value = "json") RequestBody json);

问题:

请求体是空的,当它来到的服务器

request body is empty, when it come to server

推荐答案

首先,请您尝试发送mediaType的应用程序/ JSON为@Multpart,如果你想发送multpartfile所以你需要改变出头的你code。

First all, do you are trying send the mediaType "application/json" as @Multpart, if you want send the multpartfile so do you needs change somethings in your code.

事情是这样的:

@Multipart
    @POST("/profile/update")
    Call<RegistrationResponse> updateProfile2(
    @Header(value = "X-AUTH-TOKEN") String toke,
    @Part("myfile\"; filename=\"image.png\" ") RequestBody file);

和改变调用方法如下:

RequestBody file = RequestBody.create(MediaType.parse("multipart/form-data"), myFile);
        return apiService.updateProfile2(token, file);