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:
- Created 3 years ago
- Comments:5 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

That’s right. Also if you use
getHeaderson the MockWebServer validation you’ll get a list with two values for both headers which also would have helped indicate the problem.@waqasakram117 thank you for pointing out that!