Skip to content

body

inline suspend fun <Out : Any> SpineResponse<Out, FailureSpec.Never>.body(): Out

Returns the response body.

This overload is inferred when the endpoint was declared without any possible failure types.

To declare a failure type, see failure or the failures tutorial.

context(raise1: Raise<F1>)
inline suspend fun <Out : Any, F1> SpineResponse<Out, FailureSpec.Or<FailureSpec.Never, FailureSpec.ByCode<F1>>>.body(): Out

Returns the response body of an endpoint that declares one or more failures.

If any of the declared failures happen, they are raised into the corresponding Raise context parameter.

Example

For the following API:

object Users : RootResource("users") {

    object User : DynamicResource<Users>("user", Users) {

        val get by get()
            .response<UserDto>()
            .failure<NotFound>(HttpStatusCode.NotFound)
            .failure<NotAllowedToQueryUsers>(HttpStatusCode.Forbidden)
    }

    val me by get()
        .response<UserDto>()
        .failure<NotLoggedIn>(HttpStatusCode.Unauthorized)
}

We can request the different endpoints as so:

val client = HttpClient()
// …configure your HttpClient…

context(_: Raise<NotLoggedIn>)
suspend fun HttpClient.me(): UserDto =
    request(Users / Users.me).body()

context(_: Raise<NotFound>, _: Raise<NotAllowedToQueryUsers>)
suspend fun HttpClient.getUser(id: String): UserDto =
    request(Users / User(id) / User.get).body()