-1

I am sending a POST request to server and my interface is this for Retrofit2 to convert it into JSON

public interface LoginService {

@POST("auth/signin")
@FormUrlEncoded
Call<CurrentUser> signin(
        @Field("email") String email,
        @Field("password") String password,
        @Field("device_os") String device_os,
        @Field("device_identity") String device_id
);

}

I want to see this request body in JSON format for the purpose of debugging.

Please help ! Thanks in advance .

Devansh Kumar
  • 1,552
  • 1
  • 15
  • 27

2 Answers2

2

check this answer

You can use a logging interceptor for it.

Community
  • 1
  • 1
Jose M Lechon
  • 5,766
  • 6
  • 44
  • 60
0

You can use Interceptor for OkHttpClient, then in the intercept method you would have:

...
Request.Builder builder = chain.request().newBuilder();
Request request = builder.build();
String bodyString = bodyToString(request);
...

private String bodyToString(final Request request) {
    try {
        final Request copy = request.newBuilder().build();
        final Buffer buffer = new Buffer();

        if (copy.body() != null) {
            copy.body().writeTo(buffer);
            return buffer.readUtf8();
        }

        return "";
    } catch (final IOException e) {
        return "bodyToString error.";
    }
}
Niko
  • 8,093
  • 5
  • 49
  • 85