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

dambakk

03/13/2020, 5:32 PM
Hi! I try to make a request with a body of id’s. So I have a serializable data class IdWrapper(val id: String) and set the body of the request to a list of IdWrapper instances. I though that should be it, but I get an exception saying “IdWrapper cannot be cast to String” when setting the `HttpRequestBuilder`’s body. Why is it expecting a String? Shouldn’t it accept
Any
serializable object?
I have installed the
JsonFeature
and annotated the data class with
Serializable
. I have also tried to convert the list to json manually using
Json.nonstrict.toJson(…)
but this results in a
JsonObject cannot be cast to String
exception.
c

corneil

03/13/2020, 8:09 PM
If you look at the specification HTTP bodies are
String
media type gives a hint of how to convert the string. A binary blob becomes base64 encoded string.
d

dambakk

03/16/2020, 7:56 AM
I have already set Content-Type header to application/json. Anything else I should specify? All my other requests works fine, except this one which tries to set a List of objects as body. @corneil
I got it now… I had to wrap my list of objects in another object. E.g.:
Copy code
data class IdObject(
  val id: String
)

data class IdList(
  val ids: List<IdObject>
)
Worked on first try 🙂