OkHttp how to log request body

前端 未结 5 1691
深忆病人
深忆病人 2020-12-13 11:51

I\'m using an interceptor, and I would like to log the body of a request I\'m making but I can\'t see any way of doing this.

Is it possible ?

public         


        
5条回答
  •  青春惊慌失措
    2020-12-13 12:31

    I tried to comment on the correct answer from @msung, but my reputation isn't high enough.

    Here's modification I did to print RequestBody before making it a full request. It works like a charm. Thanks

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

提交回复
热议问题