OkHttp how to log request body

Nikola’s answer did not work for me. My guess is the implementation of ByteString#toString() changed. This solution worked for me:

private static String bodyToString(final Request request){

    try {
        final Request copy = request.newBuilder().build();
        final Buffer buffer = new Buffer();
        copy.body().writeTo(buffer);
        return buffer.readUtf8();
    } catch (final IOException e) {
        return "did not work";
    }
}

From the documentation of readUtf8():

Removes all bytes from this, decodes them as UTF-8, and returns the string.

which should be what you want.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)