You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow injecting a custom ServerpodClientRequestDelegate #4850
ServerpodClientRequestDelegate is a well-designed abstraction, but ServerpodClientShared hardcodes ServerpodClientRequestDelegateImpl (which uses dart:io HttpClient) in its constructor — there's no way to inject a custom implementation.
This means Serverpod apps on mobile cannot use platform-native HTTP clients like cupertino_http (NSURLSession on iOS) or cronet_http (Cronet on Android), missing out on:
Your suggestion of allowing the usage of custom HTTP clients like cupertino_http and cronet_http is indeed valuable! However, better than exposing the internal ServerpodClientRequestDelegate would be to allow overriding the http.Client class.
Packages like the ones you mentioned already use it and expose subclasses of the http.Client, making it a clean way to integrate with platform-native HTTP clients without requiring further boilerplate from users.
Serverpod should simply use http.Client, then you could allow users to pass their own client on initialization
Am 20. März 2026, 20:51 -0500 schrieb Marcelo Mendonça Soares ***@***.***>:
marcelomendoncasoares left a comment (serverpod/serverpod#4850)
Hi @escamoteur! 👋
Your suggestion of allowing the usage of custom HTTP clients like cupertino_http and cronet_http is indeed valuable! However, better than exposing the internal ServerpodClientRequestDelegate would be to allow overriding the http.Client class.
Packages like the ones you mentioned already use it and expose subclasses of the http.Client, making it a clean way to integrate with platform-native HTTP clients without requiring further boilerplate from users.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you were mentioned.Message ID: ***@***.***>
Serverpod should simply use http.Client, then you could allow users to pass their own client on initialization
This is precisely the idea. On web we already use http.Client (the serverpod_client_browser.dart file), but a refactor is needed to use it on IO as well (serverpod_client_io.dart). Then, we can expose for users to override the http.Client with any custom implementation.
Solution for harmonizing the contract between IO and web: d4d546a.
With this, we'll be able to expose a http.Client httpClientOverride parameter on the generated Client class that will allow users to override with any custom http.Client (like the cupertino_http and cronet_http).
Not really an issue, but there is an inconsistency in the message, i.e., one is 'Unknown server response code. ($e)' and the other is e.toString() directly, which may make parsing the underlying SocketException less straightforward, although at least all use -1 status code.
I think the ideal solution is to not expose statusCode by default, instead use a sealed class, and map SocketException properly in a type-safe way, so statusCode is only available for HTTP client failures. Example.
In this example, it also handles the case where JSON is malformed (may happen when using Cloudflare, Nginx, or some reverse proxy that may return HTML in case of server issues) and in case the JSON structure is incompatible (important for backward compatibility for self-hosted servers, where the server deployment may be controlled by the server, like WordPress).
Activity
marcelomendoncasoares commentedon Mar 21, 2026
Hi @escamoteur! 👋
Your suggestion of allowing the usage of custom HTTP clients like
cupertino_httpandcronet_httpis indeed valuable! However, better than exposing the internalServerpodClientRequestDelegatewould be to allow overriding thehttp.Clientclass.Packages like the ones you mentioned already use it and expose subclasses of the
http.Client, making it a clean way to integrate with platform-native HTTP clients without requiring further boilerplate from users.escamoteur commentedon Mar 21, 2026
marcelomendoncasoares commentedon Mar 26, 2026
This is precisely the idea. On web we already use
http.Client(theserverpod_client_browser.dartfile), but a refactor is needed to use it on IO as well (serverpod_client_io.dart). Then, we can expose for users to override thehttp.Clientwith any custom implementation.marcelomendoncasoares commentedon May 14, 2026
Solution for harmonizing the contract between IO and web: d4d546a.
With this, we'll be able to expose a
http.Client httpClientOverrideparameter on the generatedClientclass that will allow users to override with any customhttp.Client(like thecupertino_httpandcronet_http).EchoEllet commentedon May 27, 2026
Thanks for addressing the issue!
I have noticed that #5134 handles both
SocketExceptionandClientException, and technically only one of them is needed.Not really an issue, but there is an inconsistency in the message, i.e., one is
'Unknown server response code. ($e)'and the other ise.toString()directly, which may make parsing the underlyingSocketExceptionless straightforward, although at least all use-1status code.I think the ideal solution is to not expose
statusCodeby default, instead use a sealed class, and mapSocketExceptionproperly in a type-safe way, sostatusCodeis only available for HTTP client failures. Example.Off-topic (related to #3283)
In this example, it also handles the case where JSON is malformed (may happen when using Cloudflare, Nginx, or some reverse proxy that may return HTML in case of server issues) and in case the JSON structure is incompatible (important for backward compatibility for self-hosted servers, where the server deployment may be controlled by the server, like WordPress).
Related #3283 and #3283 (comment)