Skip to content
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

ContentType for HttpClient request #635

Closed
coolemza opened this issue Oct 9, 2018 · 9 comments
Closed

ContentType for HttpClient request #635

coolemza opened this issue Oct 9, 2018 · 9 comments
Assignees
Labels
ux User Experience issue

Comments

@coolemza
Copy link

coolemza commented Oct 9, 2018

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

@e5l
Copy link
Member

e5l commented Oct 10, 2018

Hi, @coolemza. Thanks for the report.
You could set up it in OutgoingContent

@e5l e5l self-assigned this Oct 10, 2018
@e5l e5l added the ux User Experience issue label Oct 10, 2018
@gaydenko
Copy link

@e5l Please, provide an example of content type setting for particular request (rather client).

@gaydenko
Copy link

That is this fragment does work with 0.9.3, but not with > 0.9.3:

            val response = client.call(url) {
                contentType(ContentType.Application.Json)
                method = HttpMethod.Post
                body = json.writeValueAsString(userData)
            }.response

@e5l
Copy link
Member

e5l commented Oct 16, 2018

Sure :)

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

@e5l
Copy link
Member

e5l commented Oct 16, 2018

@soywiz could we have a sample for the case?

@coolemza
Copy link
Author

is it possible to set default content type for engine?

@e5l
Copy link
Member

e5l commented Oct 16, 2018

Yep. You could intercept send pipeline and wrap the body into outgoing content with selected ContentType field.

@e5l
Copy link
Member

e5l commented Oct 16, 2018

I think the problem is obsolete for now. Closed

@gsavvid
Copy link

gsavvid commented 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)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ux User Experience issue
Projects
None yet
Development

No branches or pull requests

4 participants