package com.cheng.blzb.net

import okhttp3.Interceptor
import okhttp3.Response
import java.util.Locale

class ContentTypeInterceptor : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val originalRequest = chain.request()
        val url = String.format("%s", originalRequest.url)
        val method = originalRequest.method.lowercase(Locale.getDefault()).trim { it <= ' ' }

        if (!url.contains("user/upload")) {
            if(method == "post" || method == "put"){
                val newRequest = originalRequest.newBuilder()
                    .header("Content-Type", "application/json; charset=utf-8")
                    .method(originalRequest.method, originalRequest.body)
                    .build()
                return chain.proceed(newRequest)
            }
        }
        return chain.proceed(originalRequest)
    }
}