tryPatch

inline suspend fun <B, T> HttpFetcher.tryPatch(resource: String, body: B, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, bodySerializer: SerializationStrategy<B> = serializer(), noinline transform: suspend Response.() -> T): T?(source)

A serialize-friendly version of tryPatch that accepts a serializable body and logic to convert the response to some desired target object of type T.

A transform callback is provided allowing you to convert the response into a different type. You are generally encouraged to call tryPatch(...) { convert() } over tryPatch(...)?.convert() as the former will ensure that exception handling is covered in that case.

Additionally, if HttpFetcher.logOnError is set to true, any failure will be logged to the console (including the logic in the transform block).

If you do not care about converting the result to some arbitrary type, use the tryPatch version that returns Response? instead. For serialization-aware methods, it is expected that users will rarely, if ever, need this version. Instead, via the Response version, use tryPatch<RequestClass>().bodyAs<ReplyClass>()


inline suspend fun <B> HttpFetcher.tryPatch(resource: String, body: B, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, bodySerializer: SerializationStrategy<B> = serializer()): Response?(source)

Like patch but returns null instead of throwing if the request fails.

Additionally, if HttpFetcher.logOnError is set to true, any failure will be logged to the console. By default, this will be true for debug builds and false for release builds.

If you plan to do additional operations on the response and would also like to have logging / exception protection for them, consider using the other tryPatch call which lets you pass in a transform callback. You are generally encouraged to call tryPatch(...) { convert() } over tryPatch(...)?.convert().


inline suspend fun <B, R> HttpFetcher.tryPatch(resource: String, body: B, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, bodySerializer: SerializationStrategy<B> = serializer(), responseDeserializer: DeserializationStrategy<R> = serializer()): R?(source)

Deprecated

With these serialization-aware network methods, we are moving response deserialization handling to a separate `bodyAs` call. This lets us accomplish the same amount of functionality with fewer methods.

Replace with

import com.varabyte.kobweb.browser.http.tryPatch
import com.varabyte.kobweb.browser.http.FetchDefaults
import com.varabyte.kobweb.browser.http.bodyAs
import kotlinx.serialization.serializer
tryPatch(resource, body, headers, redirect, abortController, bodySerializer) { bodyAs(responseDeserializer) }

Like patch, but returns null if the request fails or the response can't be deserialized.

Additionally, if HttpFetcher.logOnError is set to true, any failure will be logged to the console. By default, this will be true for debug builds and false for release builds.


inline suspend fun <R> HttpFetcher.tryPatch(resource: String, headers: Map<String, Any>? = FetchDefaults.Headers, redirect: RequestRedirect? = FetchDefaults.Redirect, abortController: AbortController? = null, responseDeserializer: DeserializationStrategy<R> = serializer()): R?(source)

Deprecated

With these serialization-aware network methods, we are moving response deserialization handling to a separate `bodyAs` call. This lets us accomplish the same amount of functionality with fewer methods.

Replace with

import com.varabyte.kobweb.browser.http.tryPatch
import com.varabyte.kobweb.browser.http.FetchDefaults
import com.varabyte.kobweb.browser.http.bodyAs
import kotlinx.serialization.serializer
tryPatch(resource, body = null, headers, redirect, abortController) { bodyAs(responseDeserializer) }

A serialize-friendly version of tryPatch that has no body but provides a serialized response.