restsharp/RestSharp · error · ArgumentOutOfRangeException

Request resource doesn't contain a valid scheme for an empty

Error message

Request resource doesn't contain a valid scheme for an empty base URL of the client

What it means

Thrown by DoBuildUriValidations when the client has no BaseUrl set and the request's Resource does not start with 'http'. Without a base URL the resource must itself be an absolute URL containing the scheme, otherwise the client cannot build a valid request URI.

Source

Thrown at src/RestSharp/BuildUriExtensions.cs:93

            string GetString(string name, string? value, Func<string, string>? encode) {
                var val = encode != null && value != null ? encode(value) : value;
                return val == null ? name : $"{name}={val}";
            }

            string EncodeParameter(Parameter parameter)
                => !parameter.Encode
                    ? GetString(parameter.Name!, parameter.Value?.ToString(), null)
                    : GetString(
                        client.Options.EncodeQuery(parameter.Name!, client.Options.Encoding),
                        parameter.Value?.ToString(),
                        x => client.Options.EncodeQuery(x, client.Options.Encoding)
                    );
        }
    }

    static void DoBuildUriValidations(IRestClient client, RestRequest request) {
        if (client.Options.BaseUrl == null && !request.Resource.StartsWith("http", StringComparison.InvariantCultureIgnoreCase))
            throw new ArgumentOutOfRangeException(
                nameof(request),
                "Request resource doesn't contain a valid scheme for an empty base URL of the client"
            );
    }
}

View on GitHub (pinned to 6a50821692)

Solutions

  1. Set RestClientOptions.BaseUrl to the API root (e.g. new Uri("https://api.host")).
  2. If using per-request full URLs, ensure each RestRequest.Resource starts with http:// or https://.
  3. Validate that configuration supplying BaseUrl is non-null before constructing the client.

Example fix

// before
var client = new RestClient();
var req = new RestRequest("/api/items");

// after
var client = new RestClient(new RestClientOptions("https://api.host"));
var req = new RestRequest("/api/items");
Defensive patterns

Strategy: validation

Validate before calling

if (client.Options.BaseUrl == null && !request.Resource.StartsWith("http", StringComparison.OrdinalIgnoreCase)) throw new InvalidOperationException("Set BaseUrl or use an absolute Resource URL");

Try / catch

try { await client.ExecuteAsync(request); } catch (ArgumentOutOfRangeException ex) when (ex.Message.Contains("valid scheme")) { /* set BaseUrl or make Resource absolute */ }

Prevention

When it happens

Trigger: Creating a RestClient with no BaseUrl (or BaseUrl null) and issuing a request whose Resource is a relative path like '/api/items' instead of a full absolute URL like 'https://host/api/items'.

Common situations: Forgot to set RestClientOptions.BaseUrl; intended to pass full URLs per-request but supplied a relative path; BaseUrl was conditionally set from config that resolved to null.

Related errors


AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13). Data as JSON: /api/errors/934befd54277a36b. Report an issue: GitHub.