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 in a request headers

See original GitHub issue

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 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:closed
  • Created 4 years ago
  • Reactions:11
  • Comments:18 (1 by maintainers)

github_iconTop GitHub Comments

15reactions
aajtoddcommented, May 13, 2020

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 setter

Agreed we need the ability to manually control this header for a number of reasons.

12reactions
sannysoftcommented, Jan 17, 2020

I can’t use Ktor client just because I’m unable to set Content-Type. Even your examples doesn’t work:

   val message = client.post<HelloWorld> {
      url("http://127.0.0.1:8080/")
      contentType(ContentType.Application.Json)
      body = HelloWorld(hello = "world")
   }
Read more comments on GitHub >

github_iconTop 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 >

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