package com.example.base.extensions

import okhttp3.Request
import okio.Buffer
import java.io.IOException
import java.nio.charset.Charset


fun Request.bodyStr(): String {
    val requestBody = this.body
    val buffer = Buffer()
    try {
        requestBody?.writeTo(buffer)
    } catch (e: IOException) {
        e.printStackTrace()
        return ""
    }
    //编码设为UTF-8
    var charset = Charset.forName("UTF-8")
    val contentType = requestBody?.contentType()
    if (contentType != null) {
        charset = contentType.charset(Charset.forName("UTF-8"))
    }
    return buffer.readString(charset)
}
