Getting Request body content using Retrofit 2.0 POST method

25,155

I have got it working from this link Retrofit2: Modifying request body in OkHttp Interceptor

private String bodyToString(final RequestBody request) {
            try {
                final RequestBody copy = request;
                final Buffer buffer = new Buffer();
                if (copy != null)
                    copy.writeTo(buffer);
                else
                    return "";
                return buffer.readUtf8();
            } catch (final IOException e) {
                return "did not work";
            }
        }

Retrofit dependencies i am using are

 compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta3'
    compile 'com.squareup.okhttp3:okhttp:3.0.0-RC1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.0.0-RC1'
    compile 'com.squareup.retrofit2:retrofit:2.0.0-beta3'
Share:
25,155
Chandru
Author by

Chandru

I am an elegant and enthusiastic developer who always believes the myth of working smart rather working hard.

Updated on July 09, 2022

Comments

  • Chandru
    Chandru almost 2 years

    I have a requirement to get a request body and to perform some logic operations with Retrofit 2.0 before doing enque operation. But unfortunately I am not able to get the post body content from my service call. At present after searching a lot I found only one solution like logging the request that I am posting using Retrofit 2.0 from this method by using HttpLoggingInterceptor with OkHttpClient. I am using the following code to log the request body in the Android Logcat:

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();  
           logging.setLevel(Level.BODY);
        OkHttpClient httpClient = new OkHttpClient();
        httpClient.interceptors().add(logging);
    
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    
        return retrofit.create(apiClass);
    

    Problem I am facing with the above method:

    1. Request body are seen only on the default Android Logcat like

    02-04 01:35:59.235 5007-5035/com.example.retrofitdemo D/OkHttp:{"nameValuePairs":{"id":"1","name":"chandru","type":"user"}}

    But the above method returns only response body in the logcat, I am not able to get it as either String or JSONObject. Eventhough if I could able to get the response body using HttpLoggingInterceptor, my request body will be shown in the Logcat with the tag "OkHttp" all the time even after the application goes into the production (So primarily this leads like a kind of relieving the post data in the logcat).

    My Requirement:

    I need to get the request body as String or JSONObject or whatever method without reviling the post data in the Logcat.

    What I tried:

    I tried to fetch the request body even after onResponse from Response<T> response, but though I couldn't able to get it done possible. Please find the code that I am using for this purpose as follows:

     Gson gson = new Gson();
     String responseString =  gson.toJson(response.raw().request().body());
    

    Note: The above converted request body using gson.toJson method returns only the meta data not the request post data.

    Kindly help me with this by providing your valuable tips and solutions. I have no idea how to do this. I am completely stuck up with this for the past two days. Please forgive if my question is too lengthy. Your comments are always welcome. Do let me know if my requirement is not clear. Thanks in advance.

  • Chandru
    Chandru over 8 years
    I am adding following versions in build.gradle
  • Chandru
    Chandru over 8 years
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' compile 'com.squareup.okhttp:logging-interceptor:2.6.0'
  • Rafael Lima
    Rafael Lima about 7 years
    it helped me, but why do you need to create a local variable with the param? final RequestBody copy = request;
  • silentsudo
    silentsudo about 7 years
    it was hit and try for me at that time u are free to optimize ;)
  • silentsudo
    silentsudo almost 7 years
    For people downvoting, new retrofit dependencies have been updated please check them
  • silentsudo
    silentsudo over 6 years
    @RafaelLima if you use SonarQube it suggest to operate on new filed rather than on method parameters, it suggest to keep it unmodified.
  • Hay Trần
    Hay Trần over 4 years
    Thank you a lot !