Adam Styrc
Adam Styrc

Reputation: 1537

Send json and file with Retrofit2

I used to send POST request to server with Retrofit2:

    @POST("goals")
    Call<Void> postGoal(@Body Goal goal);

where Goal was object with some String/Integer fields.

Now I need to add a photo file there. I know I need to switch to use Multipart:

    @Multipart
    @POST("goals")
    Call<Void> postGoal(
            @Part("picture") RequestBody picture
            );

//...
//Instantaining picture 
RequestBody.create(MediaType.parse("image/*"), path)

But how am I supposed to add previous fields ? In particular is there a way to add whole Goal object without dividing it for fields ?

Upvotes: 4

Views: 3741

Answers (2)

swetabh suman
swetabh suman

Reputation: 1989

for sending json and file you can follow something like this.

@Multipart
@POST("goals")
Call<JsonModel> postGoal(@Part MultipartBody.Part file, @Part("json") RequestBody json);

Now convert your Object which you want to send as a json into json using Gson. like this.

    String json = new Gson().toJson(new Goal());

  File file = new File(path);
                        RequestBody requestFile =
                                RequestBody.create(MediaType.parse("multipart/form-data"), file);

                        // MultipartBody.Part is used to send also the actual file name
                        MultipartBody.Part body =
                                MultipartBody.Part.createFormData("picture", file.getName(), requestFile);

                        // add another part within the multipart request
                        RequestBody jsonBody=
                                RequestBody.create(
                                        MediaType.parse("multipart/form-data"), json);
                        Retrofit retrofit = new Retrofit.Builder()
                                .baseUrl(url)
                                .addConverterFactory(GsonConverterFactory.create())
                                .build();
                        RestApi api = retrofit.create(RestApi.class);
                        Call<ResponseBody> call = api.upload(jsonBody, body);
                        call.enqueue(new Callback<ResponseBody>() {
                            @Override
                            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                                Log.d("onResponse: ", "success");
                            }

                            @Override
                            public void onFailure(Call<ResponseBody> call, Throwable t) {
                                Log.d("onFailure: ", t.getLocalizedMessage());
                            }
                        });

Upvotes: 1

Romain de-coster
Romain de-coster

Reputation: 41

You can add @Part with you goal like this

@Multipart
    @POST("goals")
    Call<Void> postGoal(
            @Part("picture") RequestBody picture,
            @Part("goal") RequestBody goal
            );

    //...
    //Instantaining picture 
    RequestBody.create(MediaType.parse("image/*"), path)

You can find more details : retrofit-2-how-to-upload-files-to-server

Upvotes: 1

Related Questions