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.

Can't set FormUrlEncoded header for ktor http client request

See original GitHub issue

Ktor Version

1.1.1

Ktor Engine Used(client or server and name)

Apache

JVM Version, Operating System and Relevant Context

Ubuntu 18.04 Information:Kotlin: kotlinc-jvm 1.3.11 (JRE 1.8.0_191-8u191-b12-0ubuntu0.18.04.1-b12)

Feedback

I am trying to POST a request to an API that uses Content-Type: application/x-www-form-urlencoded.

val message = client.post<Unit> {
    url(URL("https://tapi.telstra.com/v2/oauth/token"))
    contentType(ContentType.Application.FormUrlEncoded)
    body = getAuth(client_id = "overriddenkey", client_secret = "overridenpass")
}

I get the error: io.ktor.http.UnsafeHeaderException: Header Content-Type is controlled by the engine and cannot be set explicitly

I can’t figure out how to set the headers in the client constructor. The documentation https://ktor.io/clients/http-client/calls/requests.html specifies that custom headers can be set using HttpRequestBuilder.

How do I specify the contentType and set the header data? I could not figure it out from your documentation.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
tasmalinakiscommented, Jan 18, 2019

Hi, we bumped into this problem a while ago as well. Going through the code, I believe that the current way of doing this is by using FormDataContent like so (without specifying a contentType):

body = FormDataContent(parametersOf( "client_id" to "overriddenkey", "client_secret" to "overridenpass") )

Last time I checked this was not stated somewhere in the documentation though.

0reactions
EhsanMszcommented, Mar 4, 2022

@e5l I’m currently working on this. to override contentType, it must be removed from UnsafeHeadersArray in HttpHeaders.kt. Is it ok to remove this from UnsafeHeadersArray?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending a form urlencoded POST request with Ktor Client?
You can set an instance of FormDataContent to a request body to send application/x-www-form-urlencoded request.
Read more >
Making requests | Ktor
After setting up the client, you can make HTTP requests. ... To add headers to the request, you can use the following ways:....
Read more >
Sending a form urlencoded POST request with Ktor Client?
You can set an instance of FormDataContent to a request body to send application/x-www-form-urlencoded request. To create an object of FormDataContent class ...
Read more >
样例 HttpBin - Ktor
Browse the source code on GitHub: ktor-samples-httpbin ... sends a response that will include the Headers sent from the client. get("/headers") { call....
Read more >
Index - javadoc.io
accept($receiver, contentType) - Static method in class io.ktor.client.request.UtilsKt. Sets the Accept header with a specific contentType.
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