0

I want to post an image with some data like this postman image:

Inside body:

enter image description here

And inside header:

enter image description here

And I write some code for uploading image like postman but I got this error:

okhttp3.internal.http2.ConnectionShutdownException

And here is my code as below:

    File globalFileName;

    RequestBody requestBody1 = RequestBody.create(MediaType.parse("*/*"), globalFileName);

    MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("uploadFile",
            globalFileName.getName(), requestBody1);

    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
    RequestBody type = RequestBody.create(MultipartBody.FORM, "documents");
    RequestBody token = RequestBody.create(MultipartBody.FORM, "7220A3B7F8D2FD2C236092E0918B4EA3");

    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.protocols( Collections.singletonList(Protocol.HTTP_1_1) );
    Call<ServerResponse> call = getResponse.uploadFile(userToken, fileToUpload1, type, token);
    call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
            ServerResponse serverResponse = response.body();
            Log.v(TAG, "Mahdi: Retrofit 2 onResponse: 0 " + serverResponse.toString());
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {
            Log.e(TAG, "Mahdi: Retrofit 2 onFailure: ", t);
        }
    });

ApiConfig Interface code:

public interface ApiConfig {
    @Multipart
    @POST("/upload")
    Call<ServerResponse> uploadFile(
            @Header("token") String userToken,
            @Part MultipartBody.Part file,
            @Part("category") RequestBody documents,
            @Part("token") RequestBody token
    );
}

ServerResponse class code:

public class ServerResponse {
    // variable name should be same as in the json response from php  
    @SerializedName("status")
    boolean status;
    @SerializedName("message")
    String message;
    public String getMessage() {
        return message;  
    }  
    public boolean getSuccess() {
        return status;
    }  
}

AppConfig class code:

public class AppConfig {
    private static String BASE_URL = "https://taxiappapi.webfumeprojects.online";
    public static Retrofit getRetrofit() {
        return new Retrofit.Builder().baseUrl(AppConfig.BASE_URL).addConverterFactory(GsonConverterFactory.create()).build();
    }
}

And I used this package:

implementation 'com.squareup.retrofit2:retrofit:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'

please help me thanks.

4
  • AFAICS, something's wrong with your ServerResponse. The server response has: status, message, data. Use: jsonschema2pojo.org (Just copy paste the server json response) Also, you don't really have to send token in both Form and header in your requests. Commented Feb 11, 2021 at 11:06
  • Hi @ʍѳђઽ૯ท Thanks for your response, I added the data JSONobject inside ServerResponse class, but it doesn't work Commented Feb 11, 2021 at 11:17
  • And what do you mean about "token in both Form" can you explain more, please. because I have two tokens one inside Authorization and another is inside the form data Commented Feb 11, 2021 at 11:19
  • 1
    In fact, sending your token in Form data is a bad practice. You should consider sending the token only and once for all in the authorization. I'm not sure how your server works but, try to send the token only in your auth header. Commented Feb 11, 2021 at 11:24

1 Answer 1

1

The correct structure for upload image retrofit like below but in your case i thing passing token causes issue

    RequestBody requestBody1 = RequestBody.create(MediaType.parse("multipart/form- 
    data"), globalFileName);

    MultipartBody.Part fileToUpload1 = MultipartBody.Part.createFormData("uploadFile",
            globalFileName.getName(), requestBody1);

    
    RequestBody type = RequestBody.create(MediaType.parse("multipart/form-data"), 
    "documents");

    RequestBody token = RequestBody.create(MediaType.parse("multipart/form-data"), 
    "7220A3B7F8D2FD2C236092E0918B4EA3");

    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.