{"record":{"id":"176974618a862da4","repo":"restsharp/RestSharp","slug":"response-content-is-null-176974","errorCode":null,"errorMessage":"Response content is null","messagePattern":"Response content is null","errorType":"exception","errorClass":"DeserializationException","httpStatus":null,"severity":"error","filePath":"src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs","lineNumber":66,"sourceCode":"    /// </summary>\n    /// <param name=\"settings\">Json.Net serializer settings</param>\n    public JsonNetSerializer(JsonSerializerSettings settings) => _serializer = JsonSerializer.Create(settings);\n\n    public string? Serialize(object? obj) {\n        if (obj == null) return null;\n\n        using var buffer = _writerBuffer ??= new(_serializer);\n\n        _serializer.Serialize(buffer.GetJsonTextWriter(), obj, obj.GetType());\n\n        return buffer.GetStringWriter().ToString();\n    }\n\n    public string? Serialize(Parameter bodyParameter) => Serialize(bodyParameter.Value);\n\n    public T? Deserialize<T>(RestResponse response) {\n        if (response.Content == null)\n            throw new DeserializationException(response, new InvalidOperationException(\"Response content is null\"));\n\n        using var reader = new JsonTextReader(new StringReader(response.Content)) { CloseInput = true };\n\n        return _serializer.Deserialize<T>(reader);\n    }\n\n    public ISerializer   Serializer   => this;\n    public IDeserializer Deserializer => this;\n\n    public string[] AcceptedContentTypes => ContentType.JsonAccept;\n\n    public ContentType ContentType { get; set; } = ContentType.Json;\n\n    public SupportsContentType SupportsContentType => contentType => contentType.Value.Contains(\"json\");\n\n    public DataFormat DataFormat => DataFormat.Json;\n}","sourceCodeStart":48,"sourceCodeEnd":83,"githubUrl":"https://github.com/restsharp/RestSharp/blob/6a5082169257438cd085f822f050d93256a8e499/src/RestSharp.Serializers.NewtonsoftJson/JsonNetSerializer.cs#L48-L83","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nvar result = await client.GetAsync<MyDto>(request);\n\n// after\nvar resp = await client.ExecuteAsync(request);\nif (!resp.IsSuccessStatusCode || resp.Content is null)\n    return null;\nreturn Newtonsoft.Json.JsonConvert.DeserializeObject<MyDto>(resp.Content);","handlingStrategy":"validation","validationCode":"if (response.Content is null || !response.IsSuccessStatusCode) { /* do not deserialize; return default */ }","typeGuard":null,"tryCatchPattern":"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 */ }","preventionTips":["Inspect response.Content and StatusCode before deserializing.","Use non-generic ExecuteAsync when empty bodies are possible.","Handle 204/empty responses explicitly."],"tags":["json","deserialization","null-response","newtonsoft"],"backgroundTag":null,"analyzedSha":"6a5082169257438cd085f822f050d93256a8e499","analyzedAt":"2026-08-13T20:38:25.807Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}