restsharp/RestSharp · error · InvalidOperationException

Response content is null

Error message

Response content is null

What it means

Thrown by the CsvHelper deserializer when attempting to deserialize a RestResponse whose Content property is null. The CSV reader needs a string to parse, so a null body is a hard stop. Note that even when this inner InvalidOperationException is thrown, it is caught at line 62 and re-wrapped in a DeserializationException, so the caller actually sees DeserializationException.

Source

Thrown at src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs:26

public class CsvHelperSerializer(CsvConfiguration configuration) : IDeserializer, IRestSerializer, ISerializer {
    public ISerializer Serializer => this;

    public IDeserializer Deserializer => this;

    public string[] AcceptedContentTypes => [ContentType.Csv, "application/x-download"];

    public SupportsContentType SupportsContentType => x => Array.IndexOf(AcceptedContentTypes, x) != -1 || x.Value.Contains("csv");

    public DataFormat DataFormat => DataFormat.None;

    public ContentType ContentType { get; set; } = ContentType.Csv;

    public CsvHelperSerializer() : this(new(CultureInfo.InvariantCulture)) { }

    public T? Deserialize<T>(RestResponse response) {
        try {
            if (response.Content == null)
                throw new InvalidOperationException(message: "Response content is null");

            using var stringReader = new StringReader(response.Content);
            using var csvReader    = new CsvReader(stringReader, configuration);

            var @interface = typeof(T).GetInterface("IEnumerable`1");

            if (@interface == null) {
                csvReader.Read();
                return csvReader.GetRecord<T>();
            }

            var itemType = @interface.GenericTypeArguments[0];
            T   result;

            try {
                result = Activator.CreateInstance<T>();
            }
            catch (MissingMethodException) {

View on GitHub (pinned to 6a50821692)

Solutions

  1. Check response.IsSuccessStatusCode and response.Content for null before calling deserialize, or use the non-generic ExecuteAsync and deserialize manually.
  2. Verify the endpoint actually returns CSV body content for the request you made (inspect response.StatusCode).
  3. If empty bodies are expected, guard with 'if (response.Content is null) return default;' before deserializing.

Example fix

// before
var resp = await client.GetAsync<List<Foo>>(request);

// after
var resp = await client.ExecuteAsync(request);
if (!resp.IsSuccessStatusCode || resp.Content is null)
    return Array.Empty<Foo>();
var csv = new CsvHelperSerializer();
return csv.Deserialize<List<Foo>>(resp);
Defensive patterns

Strategy: validation

Validate before calling

if (response.Content is null || !response.IsSuccessStatusCode) { /* skip deserialization, return empty/default */ }

Try / catch

try { var data = csvDeserializer.Deserialize<List<T>>(response); } catch (DeserializationException ex) when (ex.InnerException is InvalidOperationException ioe && ioe.Message.Contains("Response content is null")) { /* treat as empty result */ }

Prevention

When it happens

Trigger: Calling client.ExecuteAsync<SomeType>(request) or an extension like GetAsync<T> with the CsvHelperSerializer registered, when the server returns an empty or null body (e.g. a 204 No-Content, a HEAD request, or an empty stream). response.Content is null because the raw byte stream was empty.

Common situations: Pointing the CSV deserializer at an endpoint that occasionally returns 204 No Content; consuming a paginated API whose last page is empty; the response was downloaded as bytes (RawBytes) without being decoded to a string; an upstream proxy stripping the body.

Related errors


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