2

I'm using retrofit to connect to server which needs authorization token in POST Data. Example For the request:

https://adress/inboxes
POST data:
token=jasdf807gb123uy40bviubva08sdyv123&message_id=1&

I use method like this and its works fine:

@POST("/inboxes")
    Call<InboxesResponse> getInbox(@Header("Authorization") String authorization, @Header("Content-Type") String contentType, @Body RequestBody body);

I put token and other parameters into RequestBody:

RequestBody body =
                RequestBody.create(MediaType.parse("text/plain"), text.toString());

All this works fine. But i cant figure out how to work with file uploading method:

Example:
https://adress/fileUpload
POST data:
token=jasdf807gb123uy40bviubva08sdyv123
File body

So i need to use MultipartBody.Part file to upload file, but how to combine MultipartBody with RequestBody which contains token? Or how to do this right? i'm confused..

Big Coach
  • 2,485
  • 2
  • 12
  • 31

2 Answers2

0

Ok, the answer was:

 @Multipart
    @POST("/fileUpload")
    Call<ResponseBody> uploadFile(@Header("Authorization") String authorization,
                                  @Part("token") RequestBody token,
            @Part MultipartBody.Part file
    );
Big Coach
  • 2,485
  • 2
  • 12
  • 31
0

This example worked for me.

@Multipart
@Headers("Accept: application/json")
@POST("reports/new")
suspend fun registerReport(@Header("Authorization") access_token:String,
                           @Part("description") description: RequestBody,
                           @Part photo: MultipartBody.Part
) : RegisterResponse

As you can see, is not neccesary that the access_token be of type RequestBody, you can pass the header and his type can be String.

Freddy Daniel
  • 369
  • 2
  • 16