restsharp/RestSharp · error · ArgumentException
Parameter cannot be an empty string
Error message
Parameter cannot be an empty string
What it means
Thrown by Ensure.NotEmptyString when the supplied value, after coercion to a string, is null, empty, or whitespace. This is a shared guard used across the library (e.g. ContentType construction, parameter names) to reject blank string arguments.
Source
Thrown at src/RestSharp/Ensure.cs:24
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// 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.
namespace RestSharp;
static class Ensure {
public static T NotNull<T>(T? value, [InvokerParameterName] string name) => value ?? throw new ArgumentNullException(name);
public static string NotEmptyString(object? value, [InvokerParameterName] string name) {
var s = value as string ?? value?.ToString();
if (s == null) throw new ArgumentNullException(name);
return string.IsNullOrWhiteSpace(s) ? throw new ArgumentException("Parameter cannot be an empty string", name) : s;
}
}View on GitHub (pinned to 6a50821692)
Solutions
- Ensure the argument passed to the guarded API is a non-empty, non-whitespace string.
- Validate inputs at the call site before invoking the RestSharp API.
- Supply sensible defaults when config-driven strings may be empty.
Example fix
// before var ct = (ContentType)(config.ContentType ?? ""); // empty config // after var ctStr = string.IsNullOrWhiteSpace(config.ContentType) ? "application/json" : config.ContentType; var ct = (ContentType)ctStr;
Defensive patterns
Strategy: validation
Validate before calling
if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Value must be a non-empty, non-whitespace string", nameof(value)); Type guard
static bool IsNonEmptyString(string? s) => !string.IsNullOrWhiteSpace(s);
Try / catch
try { /* RestSharp API call guarded by Ensure.NotEmptyString */ } catch (ArgumentException ex) when (ex.Message.Contains("empty string")) { /* supply a valid default */ } Prevention
- Validate config-sourced strings for null/empty/whitespace before passing to RestSharp.
- Provide defaults for optional string configuration values.
When it happens
Trigger: Calling an API that delegates to Ensure.NotEmptyString with a null, empty, or whitespace-only string argument, such as constructing a ContentType from a blank string, or passing a blank parameter name.
Common situations: Reading content-type or parameter name from config that resolved to empty; passing string.Empty inadvertently; whitespace-only values from trimmed user input.
Related errors
- value must be non-negative
- Duplicate header names exist: {duplicateKeys}
- Invalid content type string
- Request body serialized to null
- AdvancedResponseWriter is not null. Only one response writer
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/73d67cc12d595cfb.
Report an issue: GitHub.