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.

ContentType for HttpClient request

See original GitHub issue

how to setup contentType for HttpClient request?

    val client = HttpClient(OkHttp) {
        engine {
            defaultRequest {
                contentType(ContentType.Application.FormUrlEncoded)
            }
        }
    }

this code throws exception: io.ktor.http.UnsafeHeaderException: Header Content-Type is controlled by the engine and cannot be set explicitly

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:6
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

11reactions
e5lcommented, Oct 16, 2018

Sure 😃

val response = client.call(url) {
   method = HttpMethod.Post
   body = TextContent(json.writeValueAsString(userData), contentType = ContentType.Application.Json)
}.response
2reactions
gsavvidcommented, Nov 16, 2019

@e5l What if you don’t want to specify a Content-Type? There’s this scenario with direct uploads to Google Cloud Storage Buckets where adding a Content-Type header results in a signature mismatch. Here’s more info about this.

The following also fails because it sets Content-Type: null:

client.put<String> {
    url(url)
    body = ByteArrayContent(byteArray, contentType = null)
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do you set the Content-Type header for an HttpClient ...
using (var httpClient = new HttpClient()) { httpClient.BaseAddress = new Uri("http://example.com/"); httpClient.DefaultRequestHeaders.Add("Accept", ...
Read more >
How to set the Content-Type header for an HttpClient request
How to set the Content-Type header for an HttpClient request · var client = new HttpClient(); · client.BaseAddress = new Uri("http://example.com/"); · //...
Read more >
HttpClient and how to use Headers, Content-Type and ...
Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects."
Read more >
C# HttpClient - How to set the HTTP Content-Type header
This post describes how to make a HTTP request with a specific content-type for example "text/plain" using the HttpClient in C#.
Read more >
How to set the Content-Type header for an HttpClient request ...
The code for How to set the Content-Type header for an HttpClient request? HttpClient client = new HttpClient(); client.BaseAddress = new Uri ...
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