https://kotlinlang.org logo
#ktor
Title
# ktor
c

coletz

10/22/2018, 3:54 PM
How to set contentType to applicaton/json with ktor client? I tried both
headers[HttpHeaders.ContentType] = ContentType.Application.Json.toString()
and
contentType(ContentType.Application.Json)
but I get an exception:
io.ktor.http.UnsafeHeaderException: Header Content-Type is controlled by the engine and cannot be set explicitly
. How to set it with the engine? Considering I'm using ios so I'm not sure I can use ktor-client-json
📝 1
1
body = TextContent("json", ContentType.Application.Json)
seems that the doc about json in that page is outdated
c

coletz

10/22/2018, 5:11 PM
using TextContent with ContentType.Application.Json worked! Thanks
👏 1
k

ketilsan

12/03/2018, 7:55 AM
Im trying to achieve something similar, but the content-type i want to set is not recognised by ktor-contenttype-enum. If I try to set it explicitly I get the same error as Dario. I have to set the following content-type to call a external rest-endpoint:
Copy code
application/xacml+json
s

spand

12/03/2018, 7:57 AM
Maybe I am just using an old version but
ContentType
is a class here. Cant you just call the constructor with what you want it to be ?
l

lukaswelte

12/03/2018, 7:58 AM
That is a really good question @ketilsan. I solved a similar issue by creating my own
JsonFeature
(I wanted to have the result directly parsed as JSON)
k

ketilsan

12/03/2018, 8:00 AM
@spand hmm, maybe! I will try that. @lukaswelte: I will have to try that if what Johannes suggested doesn´t work
thanks for the suggestions!
Copy code
ContentType.create("myMimeType")
seems promising edit: nah, was looking at the wrong ContentType-class
c

cmgurba

12/12/2018, 8:19 PM
In those examples we are setting the content type header via body (for POST/PUT/PATCH etc), how can we set content-type for GETs given that the old way no longer works? 🤔
c

coletz

12/13/2018, 3:01 PM
Not sure on what I'm going to say, but afaik content-type is for the type of the content in the request...and with a GET there is no content at all. If you need to set the content of the response that's not done with this
228 Views