restsharp/RestSharp · error · ArgumentException

Both BaseUrl and Resource are empty

Error message

Both BaseUrl and Resource are empty

What it means

UriExtensions.MergeBaseUrlAndResource throws ArgumentException when both the client BaseUrl is null/empty and the request Resource is null/empty. RestSharp needs at least one of them to form a request URI; with neither, there is no destination to send to.

Source

Thrown at src/RestSharp/Request/UriExtensions.cs:29

// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// 

using RestSharp.Extensions;

namespace RestSharp;

static class UriExtensions {
    public static Uri MergeBaseUrlAndResource(this Uri? baseUrl, string? resource) {
        var assembled = resource;

        if (assembled.IsNotEmpty() && assembled[0] == '/') assembled = assembled[1..];

        if (baseUrl == null || baseUrl.AbsoluteUri.IsEmpty()) {
            return assembled.IsNotEmpty()
                ? new Uri(assembled)
                : throw new ArgumentException("Both BaseUrl and Resource are empty", nameof(resource));
        }

        var usingBaseUri = baseUrl.AbsoluteUri[^1] == '/' || assembled.IsEmpty() ? baseUrl : new(baseUrl.AbsoluteUri + "/");

#if NETSTANDARD2_0
        return !string.IsNullOrWhiteSpace(assembled) ? new(usingBaseUri, assembled, true) : baseUrl;
#else
        return !string.IsNullOrWhiteSpace(assembled) ? new(usingBaseUri, assembled) : baseUrl;
#endif
    }

    public static Uri AddQueryString(this Uri uri, string? query) {
        if (query == null) return uri;

        var builder = new UriBuilder(uri);
        builder.Query = builder.Query.Length > 1 ? $"{builder.Query[1..]}&{query}" : query;

        return builder.Uri;

View on GitHub (pinned to 6a50821692)

Solutions

  1. Set RestClientOptions.BaseUrl (or use the RestClient(Uri baseUrl) constructor).
  2. Provide a non-empty Resource string on the RestRequest.
  3. At minimum ensure one of BaseUrl or Resource is non-empty before calling ExecuteAsync.

Example fix

// before
var client = new RestClient(); // no BaseUrl
var req = new RestRequest();     // no Resource
await client.ExecuteAsync(req); // throws

// after
var client = new RestClient(new Uri("https://api.example.com"));
var req = new RestRequest("/items");
await client.ExecuteAsync(req);
Defensive patterns

Strategy: validation

Validate before calling

if ((client.Options.BaseUrl is null || string.IsNullOrEmpty(client.Options.BaseUrl.AbsoluteUri))
    && string.IsNullOrEmpty(request.Resource))
    throw new InvalidOperationException("Set RestClientOptions.BaseUrl or a non-empty RestRequest.Resource");

Try / catch

try {
    await client.ExecuteAsync(request);
}
catch (ArgumentException ex) when (ex.Message.Contains("BaseUrl and Resource are empty")) {
    // configure base URL/resource then retry
}

Prevention

When it happens

Trigger: Creating a RestClient without a BaseUrl (e.g. new RestClient() with default options) and executing a RestRequest whose Resource string is null or empty. Also when BaseUrl is set to an empty Uri.

Common situations: Forgetting to set BaseUrl on RestClientOptions; constructing a RestClient from an HttpClient without a base address and then issuing a request with no resource; passing a relative request with no resource expecting the BaseUrl alone to suffice but BaseUrl is missing too.

Related errors


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