bodyAs

inline suspend fun <R> Response.bodyAs(requireOk: Boolean = true, strategy: DeserializationStrategy<R> = serializer()): R(source)

Convert the receiving Response into an object of type R (where that class should be marked @Serializable).

This method will throw if the response body cannot be converted (usually a deserialization issue).

For example, if you had two serializable classes, EchoRequset and EchoReply, your calling code might look something like this:

val echoReply = window.http.post("echo", EchoRequest("hello")).bodyAs<EchoReply>()
console.log("Echo: ${echoReply.message}")

Parameters

requireOk

If true and Response.ok is false, this method will throw. Even responses that are !ok can still have bodies, so you may still want to opt in to reading the body. However, while a successful response will have a valid value designed for deserialization, error responses sometimes have generic error messages instead, which is why this value defaults to true.


inline suspend fun <R> Response.bodyAs(strategy: DeserializationStrategy<R>): R(source)