question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Content-Type set in interceptor is overwritten, maybe by ConverterFactory default

See original GitHub issue
  • Bug report.

I think its a bug, maybe not, we will see 😉 If I set the Content-Type header with chain interceptor, it will be overwritten to a default. I guess it is the ConverterFactory, but i’m not sure.

I wrote a test to show whats happening. In interceptor I set .addHeader("Content-Type", "application/vnd.x.api.v1+json") which is also logged by HttPLoggingInterceptor, but the request received by the MockServer has Content-Type: application/json; charset=UTF-8

If anything is unclear fell free to ask.


open class RetrofitBug {
    internal interface Service {
        @POST("/path")
        fun post(@Body body:PostBody): Call<ResponseBody?>
    }

    data class PostBody(val value: String)

    @Test
    @Throws(Exception::class)
    fun test() {
        val server = MockWebServer()
        val retrofit = Retrofit.Builder()
            .addConverterFactory(
                GsonConverterFactory.create(
                    GsonBuilder()
                        .create()
                )
            )
            .client(
                OkHttpClient().newBuilder()
                    .addInterceptor { chain ->
                        val newRequest = chain.request().newBuilder()
                            .header("Content-Type", "application/vnd.x.api.v1+json")
                            .build()
                        chain.proceed(newRequest)
                    }
                    .addInterceptor(HttpLoggingInterceptor { message -> println(message) }.apply {
                        level = HttpLoggingInterceptor.Level.BODY
                    })
                    .build()
            )
            .baseUrl(server.url("/"))
            .build()
        val example = retrofit.create(
            Service::class.java
        )
        server.enqueue(
            MockResponse()
                .addHeader("Content-Type", "text/plain")
                .setBody("Hi")
        )
        val call = example.post(PostBody("Test"))
        val response = call.execute()
        val recordedRequest = server.takeRequest()
        println(recordedRequest.headers) // does not show my header, but **Content-Type: application/json; charset=UTF-8**
        assertEquals("application/vnd.x.api.v1+json", recordedRequest.getHeader("Content-Type"));
        server.shutdown()
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
JakeWhartoncommented, Oct 21, 2020

That’s right. Also if you use getHeaders on the MockWebServer validation you’ll get a list with two values for both headers which also would have helped indicate the problem.

0reactions
matecodecommented, Oct 22, 2020

@waqasakram117 thank you for pointing out that!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Default methods Content-Type headers cant be overridden ...
As a workaround, i'm using an interceptor. axios.interceptors.request.use((config) => { /* using Typescript, method can be undefined */ if( ...
Read more >
How to remove Content Type set in HTTP Interceptors for ...
In my application we set content-type = application/json in interceptor. But to upload a file content-type should be multipart/form-data ...
Read more >
S-Index (Spring Framework 5.3.0 API)
Set the default character set to use for reading and writing form data when the request or response Content-Type header does not explicitly...
Read more >
Index (Struts 2 Core 2.6-SNAPSHOT API)
Provides default implementations of optional lifecycle methods. AbstractInterceptor() - Constructor for class com.opensymphony.xwork2.interceptor.
Read more >
Chapter 20. Client/Server Red Hat Data Grid 7.3
Optional name of the converter factory to be used with this listener. ... resp.body #use PUT to overwrite put = Net::HTTP::Put.new(uri.path, {"Content-Type" ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found