Home » Android » android – okhttp loggin interceptor in multipart

android – okhttp loggin interceptor in multipart

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to upload an image using okHTTP multi part into a server. the server does not accept my request. my problem is that I cannot see the parts of my multi part in the logging intercepter, so I cannot debug. here is the code that I use and the reulting log cat and the desired payload. any help is appreciated.

            RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("uploaded_file", filename, RequestBody.create(MEDIA_TYPE_PNG, sourceFile))
                .addFormDataPart("flowChunkNumber", "1")
                .addFormDataPart("flowCurrentChunkSize", String.valueOf(sourceFile.getTotalSpace()))
                .addFormDataPart("flowChunkSize", "1048576")
                .addFormDataPart("flowIdentifier", "4731-images1jpeg")
                .addFormDataPart("flowFilename", "images (1).jpeg")
                .addFormDataPart("flowFilename", "images (1).jpeg")
                .addFormDataPart("flowRelativePath", "images (1).jpeg")
                .addFormDataPart("flowTotalChunks", "1")
                .build();

        Request request = new Request.Builder()
                .addHeader("cookie", ******* )
                .url(URL_UPLOAD_IMAGE)
                .post(requestBody)
                .build();

        HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor();
        logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient client = new OkHttpClient
                .Builder()
                .addNetworkInterceptor(logInterceptor)
                .build();


        Response response = client.newCall(request).execute();

and this is what I see in the log cat when I use an interceptor

D/OkHttp: –> POST https://www.appido.ir/api/profile/avatar http/1.1

D/OkHttp: Content-Type: multipart/form-data;
boundary=a8028055-3a30-4942-916b-af56935e8b32

D/OkHttp: Content-Length: 14097
D/OkHttp: cookie: ************************
Domain=.appido.ir; expires=Tue, 23-Aug-2016 13:31:11 GMT; Path=/

D/OkHttp: Host: www.appido.ir

D/OkHttp: Connection: Keep-Alive

D/OkHttp: Accept-Encoding: gzip

D/OkHttp: User-Agent: okhttp/3.4.1

D/OkHttp:
h������gTRC���������lumi���� 07-25 07:47:49.163
7776-8509/com.androidbuts.uploadimage D/OkHttp: |������meas���� 07-25
07:47:49.163 7776-8509/com.androidbuts.uploadimage D/OkHttp:
�������$bkpt���� 07-25 07:47:49.163
7776-8509/com.androidbuts.uploadimage D/OkHttp: �������rXYZ����

and this gibberish goes on for many lines

D/OkHttp: –> END POST (14097-byte body)

How can I see a meaningful log cat? I want to acihieve this :

——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowChunkNumber”

1
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowChunkSize”

1048576
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowCurrentChunkSize”

23016
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowTotalSize”

23016
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowIdentifier”

23016-60x60music2_smalljpg
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowFilename”

60x60music2_small.jpg
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowRelativePath”

60x60music2_small.jpg
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”flowTotalChunks”

1
——WebKitFormBoundaryJDdhM3Si8enJZABA Content-Disposition: form-data; name=”file”; filename=”blob” Content-Type:
application/octet-stream

——WebKitFormBoundaryJDdhM3Si8enJZABA–

How to&Answers:

The way to see the contents of a Multipart upload from Okttp and Retrofit is to use Stetho, by Facebook, and add StethoInterceptor to the Okhttp logger

https://github.com/facebook/stetho

It puts the Android app’s network requests in your dev machine’s chrome inspector.

Answer:

it seems to be a bug in the logging intercepter.

I was able to solve this issue buy debugging the logging intercepter.
the problem is the body for multi part is too big and the itercepter cannot display such a big body all at once. it needs to be printed line by line.
here is the modified version on logging intercepter that works with multi part in okhttp 3.4.1 :

}