restsharp/RestSharp · error · DeserializationException

Response content is null

Error message

Response content is null

What it means

Thrown by the Newtonsoft.Json serializer adapter when deserializing a response whose Content is null. Unlike the CSV path, this is thrown directly wrapped inside a DeserializationException via the constructor argument, so the caller observes a DeserializationException whose inner exception is this InvalidOperationException.

Source

Thrown at src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs:66

    /// </summary>
    /// <param name="settings">Json.Net serializer settings</param>
    public JsonNetSerializer(JsonSerializerSettings settings) => _serializer = JsonSerializer.Create(settings);

    public string? Serialize(object? obj) {
        if (obj == null) return null;

        using var buffer = _writerBuffer ??= new(_serializer);

        _serializer.Serialize(buffer.GetJsonTextWriter(), obj, obj.GetType());

        return buffer.GetStringWriter().ToString();
    }

    public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);

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

        using var reader = new JsonTextReader(new StringReader(response.Content)) { CloseInput = true };

        return _serializer.Deserialize<T>(reader);
    }

    public ISerializer   Serializer   => this;
    public IDeserializer Deserializer => this;

    public string[] AcceptedContentTypes => ContentType.JsonAccept;

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

    public SupportsContentType SupportsContentType => contentType => contentType.Value.Contains("json");

    public DataFormat DataFormat => DataFormat.Json;
}

View on GitHub (pinned to 6a50821692)

Solutions

  1. Use the non-generic ExecuteAsync and inspect response.Content for null and response.IsSuccessStatusCode before deserializing.
  2. Confirm the endpoint returns a JSON body for the given input (check StatusCode and raw Content).
  3. Guard empty-body responses by returning default(T) when Content is null.

Example fix

// before
var result = await client.GetAsync<MyDto>(request);

// after
var resp = await client.ExecuteAsync(request);
if (!resp.IsSuccessStatusCode || resp.Content is null)
    return null;
return Newtonsoft.Json.JsonConvert.DeserializeObject<MyDto>(resp.Content);
Defensive patterns

Strategy: validation

Validate before calling

if (response.Content is null || !response.IsSuccessStatusCode) { /* do not deserialize; return default */ }

Try / catch

try { var dto = jsonNetSerializer.Deserialize<MyDto>(response); } catch (DeserializationException ex) when (ex.InnerException is InvalidOperationException ioe && ioe.Message.Contains("Response content is null")) { /* handle empty body */ }

Prevention

When it happens

Trigger: Calling ExecuteAsync<T> / GetAsync<T> etc. with the JsonNetSerializer registered, where the HTTP response has a null body (empty content stream, 204 No Content, or a response consumed as raw bytes only).

Common situations: Server returns 204 No Content on a DELETE/PUT; endpoint returns empty body on a not-found; response.Content never populated because an interceptor consumed the stream; HEAD request.

Related errors


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