How to set contentType to applicaton/json with kto...
# ktor
c
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
using TextContent with ContentType.Application.Json worked! Thanks
👏 1
k
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
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
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
@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
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
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
556 Views