Can't set FormUrlEncoded header for ktor http client request
See original GitHub issueKtor 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:
- Created 5 years ago
- Reactions:1
- Comments:12 (2 by maintainers)
Top 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 >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
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.
@e5l I’m currently working on this. to override
contentType
, it must be removed fromUnsafeHeadersArray
inHttpHeaders.kt
. Is it ok to remove this fromUnsafeHeadersArray
?