square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
43.09k stars 7.3k forks source link

How to enable transparent headers with okhttp #921

Closed nschwermann closed 9 years ago

nschwermann commented 9 years ago

I am reading everywhere that retrofit/okhttp is supposed to transparently handle various headers such as requesting gzip, setting the user agent, cookies etc. However, with this basic kotlin code below I'm not getting any request headers set other than the one I'm setting with the request interceptor. Is there something I need to do to enable this stuff?

    compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'

object OkConfig{

    private var client : OkHttpClient by Delegates.notNull()

    public fun client() : com.squareup.okhttp.OkHttpClient {
        return client
    }

    public fun init(context : Context){
        val cacheDir : File? = try {
            File(context.getCacheDir(), "okhttp-cache")
        } catch(e : Exception){
            error(e.getStackTraceString())
            null
        }
        client = with(OkHttpClient()){
            if(cacheDir != null) setCache(Cache(cacheDir, 10 * 1024 * 1024))
            setCookieHandler(CookieHandler.getDefault())
            setConnectTimeout(30, TimeUnit.SECONDS)
            setReadTimeout(30, TimeUnit.SECONDS)
            this
        }
    }
}

public interface VoatService{

    public companion object : AnkoLogger{

        private val ENDPOINT_URL : String = BuildConfig.BASE_VOAT_URL

        public val newInstance : VoatService
            get(){
                val restBuilder = RestAdapter.Builder();
                restBuilder.setEndpoint(ENDPOINT_URL)
                restBuilder.setClient(OkClient(OkConfig.client()))
                restBuilder.setLogLevel(RestAdapter.LogLevel.HEADERS)
                restBuilder.setRequestInterceptor(object : RequestInterceptor{
                    override fun intercept(request: RequestInterceptor.RequestFacade?) {
                        request?.addHeader("Voat-ApiKey", BuildConfig.VOAT_PUB_KEY)
                    }
                })
                return restBuilder.build().create(javaClass<VoatService>())
            }
    }

    /**
     * Gets submissions for a subverse. Supports Search Options querystring arguments.
     *
     * @param subverse The subverse in which to retrieve submissions. Special subverses include: _front (currently logged in users front page according to subscriptions), _default (voat's default subverses), _all (/v/all submissions)
     */
    @GET("/api/v1/v/{subverse}")
    public fun getSubverseSubmissions(Path("subverse") subverse : String, Query("search") search : String? = null) : Observable<SubmissionsResponse>
}
swankjesse commented 9 years ago

Where are you observing the lack of request headers? We're sending 'em

nschwermann commented 9 years ago

In logcat it only shows the header that I am adding with the intercepter, also the server isn't sending back the response body gzipped unless I add the accept-encoding header myself in the intercepter and then I also see it in logcat.

Edit: The transparent headers should show up in the logs with log level headers correct? If my log messages or complete source can help I am happy to share them.

On Thu, Jun 25, 2015 at 4:50 PM, Jesse Wilson notifications@github.com wrote:

Where are you observing the lack of request headers? We're sending 'em

— Reply to this email directly or view it on GitHub https://github.com/square/retrofit/issues/921#issuecomment-115409024.

Nathan Schwermann 785-312-0080

JakeWharton commented 9 years ago

If you are logging with Retrofit you will never see this in action. It happens in the HTTP client and truly embodies the word of "transparent" to Retrofit.

On Thu, Jun 25, 2015, 2:52 PM Nathan Schwermann notifications@github.com wrote:

In logcat it only shows the header that I am adding with the intercepter, also the server isn't sending back the content gzipped unless I add the accept-encoding header myself in the intercepter and then I also see it in logcat.

On Thu, Jun 25, 2015 at 4:50 PM, Jesse Wilson notifications@github.com wrote:

Where are you observing the lack of request headers? We're sending 'em

— Reply to this email directly or view it on GitHub https://github.com/square/retrofit/issues/921#issuecomment-115409024.

Nathan Schwermann 785-312-0080

— Reply to this email directly or view it on GitHub https://github.com/square/retrofit/issues/921#issuecomment-115409873.

nschwermann commented 9 years ago

I see, it could be helpful to add an additional log level or something to reveal them for debugging purposes. I hooked though Charles and I see that they are there and that the response headers are also altered by retrofit/okhttp so this is why I thought gzip wasn't happening. Thanks for the explanation.

JakeWharton commented 9 years ago

It is impossible to see them in Retrofit.

nschwermann commented 9 years ago

Ok thank you. Is there a list of all of the transparent things? Is my setting up the cookie store, cache, etc in the above example even necessary? Will I still get these transparent features when I plug okhttp into the facebook's fresco image library or is this all strictly from retrofit?

JakeWharton commented 9 years ago

No there is not a list. Yes you need to configure those things yourself, they don't have defaults. Any code using OkHttp's API gets the transparent gzip.