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
- Use the non-generic ExecuteAsync and inspect response.Content for null and response.IsSuccessStatusCode before deserializing.
- Confirm the endpoint returns a JSON body for the given input (check StatusCode and raw Content).
- 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
- Inspect response.Content and StatusCode before deserializing.
- Use non-generic ExecuteAsync when empty bodies are possible.
- Handle 204/empty responses explicitly.
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
- Response content is null
- The type must contain a public, parameterless constructor.
- If the type implements IEnumerable<T>, then it must contain
- Class cannot have two properties marked with SerializeAs(Con
- Couldn't parse the value of '{value}' into the '{prop.Name}'
AI-assisted analysis of restsharp/RestSharp@6a50821692 (2026-08-13).
Data as JSON: /api/errors/176974618a862da4.
Report an issue: GitHub.