Skip to content

Allow injecting a custom ServerpodClientRequestDelegate #4850

Closed

Description

@escamoteur

Problem

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:

Proposed Solution

Allow passing a custom ServerpodClientRequestDelegate to the client constructor. The current hardcoded instantiation in serverpod_client_shared.dart:

_requestDelegate = ServerpodClientRequestDelegateImpl(
  connectionTimeout: this.connectionTimeout,
  serializationManager: serializationManager,
  securityContext: securityContext,
);

Could become:

_requestDelegate = requestDelegate ?? ServerpodClientRequestDelegateImpl(
  connectionTimeout: this.connectionTimeout,
  serializationManager: serializationManager,
  securityContext: securityContext,
);

The abstraction already exists — it just needs to be exposed.

Activity

  1. marcelomendoncasoares commented on Mar 21, 2026

    @marcelomendoncasoares
    Collaborator

    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.

  2. added
    in triage
    area: client
    on Mar 21, 2026
  3. escamoteur commented on Mar 21, 2026

    @escamoteur
    Author
  4. marcelomendoncasoares commented on Mar 26, 2026

    @marcelomendoncasoares
    Collaborator

    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.

  5. moved this from Inbox 📫 to Planned 🔭 in Serverpod Frameworkson Mar 26, 2026
  6. added
    enhancement
    and removed
    in triage
    on Mar 26, 2026
  7. moved this from Planned 🔭 to In Progress 🔥 in Serverpod Frameworkson May 14, 2026
  8. added
    stake: cloud
    on May 14, 2026
  9. marcelomendoncasoares commented on May 14, 2026

    @marcelomendoncasoares
    Collaborator

    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).

  10. moved this from In Progress 🔥 to Done 🚀 in Serverpod Frameworkson May 18, 2026
  11. EchoEllet commented on May 27, 2026

    @EchoEllet
    Contributor

    Thanks for addressing the issue!

    I have noticed that #5134 handles both SocketException and ClientException, 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 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.

    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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: clientenhancementstake: cloud

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions