1

I successfully created one demo to upload image via API. If I select mobile screenshot, it is happily working. but If I choose large file then it's not working and the exception is: 413

onResponse: [size=208 text=<html>\r\n<head><title>413 Request Entity Too Large</title></head>…]

So, I searched this problem and found thousands of solution. Everyone is saying same thing. Please make a configuration on server side to extend limit

But my sweet and simple question is: Whichever image is not working with retrofit, same I tried with postman and it is working! How? And it means there is no additional need to do configuration in web server.

Now, I've doubt that might be Retrofit itself is not allowing this much large files. Then should I try with AsyncTask or may be Volley?

Code: Retrofit Client:

package com.androidbuts.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


/**
 * @author Pratik Butani
 */
public class RetroClient {

    /**
     * Upload URL of your folder with php file name...
     * You will find this file in php_upload folder in this project
     * You can copy that folder and paste in your htdocs folder...
     */
    private static final String ROOT_URL = "https://b2cprintappstg.e-arc.com/microservice/";
    /**
     * Get Retro Client
     *
     * @return JSON Object
     */

    static Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    public RetroClient() {

    }

    private static Retrofit getRetroClient() {
        return new Retrofit.Builder()
                .baseUrl(ROOT_URL)
                .client(getHeader())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
    }

    public static ApiService getApiService() {
        return getRetroClient().create(ApiService.class);
    }

    public static OkHttpClient getHeader() {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okClient = new OkHttpClient.Builder()
                .readTimeout(100, TimeUnit.SECONDS)
                .connectTimeout(100, TimeUnit.SECONDS)
                .writeTimeout(100, TimeUnit.SECONDS)
                .addInterceptor(interceptor)
                .addNetworkInterceptor(
                        new Interceptor() {
                            @Override
                            public Response intercept(Interceptor.Chain chain) throws IOException {
                                Request request = null;


                                Request original = chain.request();
                                // Request customization: add request headers
                                Request.Builder requestBuilder = original.newBuilder()
                                        .addHeader("ClientID", "xxxxxx")
                                        .addHeader("x-access-token", "3uYch6X1i2OuRd3DvBPvvwiMRqPboBdRt/PbSiP0KFB4eaQQFg==")
                                        .addHeader("PartnerID", "fV17XLswDUwlU9q9ofx4pkhezw==");


                                request = requestBuilder.build();

                                return chain.proceed(request);
                            }
                        })
                .build();
        return okClient;

    }
}

API service:

public interface ApiService {

    /*
    Retrofit get annotation with our URL
    And our method that will return us the List of Contacts
    */
    @Multipart
    @POST("user/new/api/design/files")

    Call<ResponseBody> uploadImage(@Part MultipartBody.Part file,
                                   @Part("design_id ") String designId);
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98

2 Answers2

0

I think the culprit here is the timeout that you have set for the OkHttp. I would say set the timeout to have a larger value as required.

OkHttpClient okClient = new OkHttpClient.Builder()
    .readTimeout(600, TimeUnit.SECONDS)
    .connectTimeout(600, TimeUnit.SECONDS)
    .writeTimeout(600, TimeUnit.SECONDS)
    .addInterceptor(interceptor)
    .addNetworkInterceptor(
            new Interceptor() {
                @Override
                public Response intercept(Interceptor.Chain chain) throws IOException {
                    Request request = null;


                    Request original = chain.request();
                    // Request customization: add request headers
                    Request.Builder requestBuilder = original.newBuilder()
                            .addHeader("ClientID", "xxxxxx")
                            .addHeader("x-access-token", "3uYch6X1i2OuRd3DvBPvvwiMRqPboBdRt/PbSiP0KFB4eaQQFg==")
                            .addHeader("Content-type", "multipart/form-data; boundary=" + "SomeRandomConstant");
                            .addHeader("PartnerID", "fV17XLswDUwlU9q9ofx4pkhezw==");


                    request = requestBuilder.build();

                    return chain.proceed(request);
                }
            })
    .build();

Moreover, you might consider setting the log level in the interceptor to NONE as suggested here.

interceptor.setLevel(HttpLoggingInterceptor.Level.NONE);

I am quoting JakeWharton from the issue mentioned above.

By calling .setLogLevel(RestAdapter.LogLevel.FULL) you are forcing Retrofit to buffer the entire request body into memory so that it can log. That's what the call to readBodyToBytesIfNecessary in the stack trace is doing.

Enabling logging like this should only be done when debugging.

Also, please make sure you have added the right Content-Type in your request header.

.addHeader("Content-type", "multipart/form-data; boundary=" + "SomeRandomConstant");

Please see the answer here to get more information about setting the content-type.

I hope that helps!

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Hey I forgot to mention, that I already tried with MINUTES. and Also I just tried again with 600 seconds and that logging level –  Apr 23 '20 at 18:15
  • I see. It looks like you did not set the content-type for your request. Please see the updated answer. – Reaz Murshed Apr 23 '20 at 19:29
  • randomconstant string or int? –  Apr 23 '20 at 20:03
  • String @ShefaliSingh – Reaz Murshed Apr 23 '20 at 20:05
  • as a String? @ShefaliSingh – Reaz Murshed Apr 23 '20 at 20:37
  • Even I'm trying with Volley right now, but This is the first time I'm using volley, so I just make setup. 404 coming. –  Apr 23 '20 at 20:55
  • @ShefaliSingh can you please post the URL, Headers, Body from your Postman. I think you are missing some keys. Also, please post the log that you see in your logcat. Do you see any error regarding SSL handshake? – Reaz Murshed Apr 23 '20 at 21:09
  • See, I'm not missing anything. And there is no meaning to post details here. Because If I was missing something then why small images are working? Also I just now successfully made demo on Volley. There also same thing happened. There are small size images working, and for large size image it is showing 413. Also for Retrofit error, I already posted in question. And there is no other exception. Only 413 is coming! –  Apr 23 '20 at 21:13
  • @ShefaliSingh where are you setting the Multipart body? I do not see that you are sending the file using multipart. Can you please see this answer here and upload your images using multi-part? https://stackoverflow.com/a/35175692/3145960 – Reaz Murshed Apr 23 '20 at 22:55
  • I not added code. Because when I call uploadImage, that time I'm passing file which is selected from mobile. it's not that issue.everything going correctly, but when I select large size image then only I'm getting problem. –  Apr 24 '20 at 22:53
  • Can you help this please: https://stackoverflow.com/questions/61661282/how-to-delete-existing-events-using-eventid-through-google-calendar-api-in-andro?noredirect=1&lq=1 –  May 08 '20 at 13:33
  • Can you help? Can you help? https://stackoverflow.com/questions/61843230/how-stick-to-with-adapter-position-while-its-scrolling-in-android/ –  May 17 '20 at 14:03
  • @ShefaliSingh I put an answer in your other question. Please check and let me know if that helps! – Reaz Murshed May 18 '20 at 19:14
0

try checking ngnix server. and Add configuration

Priyanka Singh
  • 40
  • 1
  • 14