-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Content-Type in a request headers #1127
Comments
As an additional information, I can solve this issue by using |
Hi @mister11, thanks for the report. Could you check and report if it solves your problem? |
Not sure if I'm doing something wrong, but this is not working: val client = HttpClient(Apache) {
install(JsonFeature) {
serializer = GsonSerializer()
acceptContentTypes = acceptContentTypes + listOf(ContentType.parse("application/vnd.api+json; ext=bulk"))
}
} This just sets To make it more clear, here is a working version of OkHttp interceptor implementation: fun provideHeadersInterceptor() = Interceptor { chain ->
val requestBuilder = chain.request().newBuilder()
.addHeader("Content-Type", "application/vnd.api+json")
chain.proceed(requestBuilder.build())
} fun provideOkHttpClient(): HttpClient = HttpClient(OkHttp) {
engine {
addNetworkInterceptor(provideHeadersInterceptor())
}
} |
any solutions? I am in the same situation, but for me even interceptior does not seem to work as I would expected and I'm getting
|
I can't use Ktor client just because I'm unable to set Content-Type. Even your examples doesn't work:
|
@sannysoft , the same doesn't work for me neither. But I found this post - and I can admit, this workaround works. But... it's a workaround :( (#635)
|
Ali-OSS also encountered this problem. We need the ability to manually control any headers including |
Why can't clients control It's also confusing since the public API of Agreed we need the ability to manually control this header for a number of reasons. |
any solutions guys? |
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks. |
any solutions guys? |
Ran into the same problem. Found out that I was use |
I'm also facing this issue. The api I'm calling REQUIRES Content-Type header in a GET request (yes I know why?), otherwise it returns error. I'm currently using another http library, but would like to move my library to Multiplatform and Ktor seems to be a good option. Is content-type blocked in Ktor GET for a reason? Are there any plans to allow it? |
bump on this. I'm facing the same issue, where the get requires the content-type be specified |
Also blocked by this. Isn't this a normal thing to do? Why isn't it allowed... |
Oh dear, I am blocked on my PUT request on KMM Android Project! Any resolution guys? |
I've solved this problem by registering my custom content types using the ContentNegotiation plugin in a fairly simple way at least for binary content. I think the ktor documentation should contain an example like this since this is a fairly common use case. example: wireapp/kalium#324 |
On YouTrack this is marked as fixed, but I was still unable to set Content-Type header to GET request using ktor 2.0.3. class EmptyContentWithContentType(
override val contentType: ContentType
) : OutgoingContent.NoContent() {
override val contentLength: Long = 0
override fun toString(): String = "EmptyContent(contentType='$contentType')"
} And then in request builder: setBody(EmptyContentWithContentType(ContentType("application", "definitely.not.json"))) |
Still no solution? |
Yep no solution we need a |
@rsinukov could you check if we can do something? |
Having conducted some investigation, I realized that ktor erases the
Nevertheless, this header is appended to the request when ktor request is being converted to the engine request. E.g., in OkHttp it's done using
In the end, I believe |
@sud007 Can you please elaborate on what doesn't work? This test passes:
|
@rsinukov Please try adding content negotiation plugin
And sending post request with any
The test you've provided will fail (assert block should be updated to application/json):
|
@flaringapp But isn't it only relevant for |
real question is what is the reason of removing the header if it was set by the user? |
@rsinukov Yes, you are right. In the real request in will be present. I just don't want to say that the issue is explicitly with the |
Another question is why to completely delegate header appending to an engine. Is it feasible to keep the header by default, and let any engine override/remove it if necessary? |
You are able to access it through @kalgecin @flaringapp Can you elaborate on what problems it causes? |
@rsinukov in my case, a test with MockEngine that verifies Content-Type header was failing. As you stated. |
@rsinukov As several people have mentioned, some APIs for some reason require the header present, otherwise they return an error. And using such APIs with ktor is impossible because ktor removes the header. I'm still to understand the reason why ktor does not want to send the header through |
@kalgecin Can you show me a reproducer where the client doesn't send header to a server, please? I fail to create one. |
@rsinukov it works but I am questioning a behavior. |
So there is no way to send the (extremely common) |
Ktor Version
1.1.4
Ktor Engine Used(client or server and name)
Apache
JVM Version, Operating System and Relevant Context
1.8, macOS Mojave and Linux Mint, IDEA 2019.1.2 CE
Feedback
I'm accessing and API that requires me to set
Content-Type
for all requests, but I'm gettingio.ktor.http.UnsafeHeaderException: Header Content-Type is controlled by the engine and cannot be set explicitly
Current implementation:
The text was updated successfully, but these errors were encountered: