Content-Type in a request headers
See original GitHub issueKtor 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 getting io.ktor.http.UnsafeHeaderException: Header Content-Type is controlled by the engine and cannot be set explicitly
Current implementation:
val client = HttpClient(Apache) {
install(JsonFeature) {
serializer = GsonSerializer()
}
install(Logging) {
level = LogLevel.HEADERS
}
defaultRequest {
header("Content-Type", "application/vnd.api+json")
}
}
get("/test") {
client.get<String>("url...") {
headers {
// other headers
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:18 (1 by maintainers)
Top Results From Across the Web
Content-Type - HTTP - MDN Web Docs - Mozilla
The Content-Type representation header is used to indicate the original media type of the resource (prior to any content encoding applied ...
Read more >Mastering the Content-Type HTTP Header - Testsuite
The Content-Type header is used in web requests to indicate what type of media or resource is being used in the request or...
Read more >How do you set the Content-Type header for an HttpClient ...
Misused header name. Make sure request headers are used with HttpRequestMessage , response headers with HttpResponseMessage , and content headers with ...
Read more >What is the Content-Type Header? - Seobility Wiki
The content-type header indicates the type of media/file in communication between the HTTP client and the server. It helps the browser or server...
Read more >Content-Type: HTTP entity-header - IBM
On an HTTP POST request, specify either the Content-Type or the x-msg-class. If you specify both, they must be consistent or an HTTP...
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 FreeTop 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
Top GitHub Comments
Why can’t clients control
Content-Type
header? I’m not sure I understand why the engine “has” to.It’s also confusing since the public API of
HttpMessageBuilder
includes a setterAgreed we need the ability to manually control this header for a number of reasons.
I can’t use Ktor client just because I’m unable to set Content-Type. Even your examples doesn’t work: