{"record":{"id":"3dfb2a2dce4e1fd4","repo":"restsharp/RestSharp","slug":"response-content-is-null","errorCode":null,"errorMessage":"Response content is null","messagePattern":"Response content is null","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs","lineNumber":26,"sourceCode":"public class CsvHelperSerializer(CsvConfiguration configuration) : IDeserializer, IRestSerializer, ISerializer {\n    public ISerializer Serializer => this;\n\n    public IDeserializer Deserializer => this;\n\n    public string[] AcceptedContentTypes => [ContentType.Csv, \"application/x-download\"];\n\n    public SupportsContentType SupportsContentType => x => Array.IndexOf(AcceptedContentTypes, x) != -1 || x.Value.Contains(\"csv\");\n\n    public DataFormat DataFormat => DataFormat.None;\n\n    public ContentType ContentType { get; set; } = ContentType.Csv;\n\n    public CsvHelperSerializer() : this(new(CultureInfo.InvariantCulture)) { }\n\n    public T? Deserialize<T>(RestResponse response) {\n        try {\n            if (response.Content == null)\n                throw new InvalidOperationException(message: \"Response content is null\");\n\n            using var stringReader = new StringReader(response.Content);\n            using var csvReader    = new CsvReader(stringReader, configuration);\n\n            var @interface = typeof(T).GetInterface(\"IEnumerable`1\");\n\n            if (@interface == null) {\n                csvReader.Read();\n                return csvReader.GetRecord<T>();\n            }\n\n            var itemType = @interface.GenericTypeArguments[0];\n            T   result;\n\n            try {\n                result = Activator.CreateInstance<T>();\n            }\n            catch (MissingMethodException) {","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp.Serializers.CsvHelper/CsvHelperSerializer.cs#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check response.IsSuccessStatusCode and response.Content for null before calling deserialize, or use the non-generic ExecuteAsync and deserialize manually.","Verify the endpoint actually returns CSV body content for the request you made (inspect response.StatusCode).","If empty bodies are expected, guard with 'if (response.Content is null) return default;' before deserializing."],"exampleFix":"// before\nvar resp = await client.GetAsync<List<Foo>>(request);\n\n// after\nvar resp = await client.ExecuteAsync(request);\nif (!resp.IsSuccessStatusCode || resp.Content is null)\n    return Array.Empty<Foo>();\nvar csv = new CsvHelperSerializer();\nreturn csv.Deserialize<List<Foo>>(resp);","handlingStrategy":"validation","validationCode":"if (response.Content is null || !response.IsSuccessStatusCode) { /* skip deserialization, return empty/default */ }","typeGuard":null,"tryCatchPattern":"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 */ }","preventionTips":["Always check response.Content for null before deserializing.","Use the non-generic ExecuteAsync to inspect the raw response first.","Handle 204 No Content responses explicitly in your pipeline."],"tags":["csv","deserialization","null-response","csvhelper"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}