From 9c3b36f9ae220919a480a020bba30ff46aa9100d Mon Sep 17 00:00:00 2001 From: Bastian Eicher Date: Sun, 7 Jul 2024 00:10:40 +0200 Subject: [PATCH] Do not attempt to parse response bodies on HTTP 204 --- src/TypedRest/Endpoints/Generic/ElementEndpoint.cs | 2 ++ src/TypedRest/Http/ResponseCache.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs b/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs index 43311d49..5bf8110b 100644 --- a/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs +++ b/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs @@ -129,6 +129,8 @@ public virtual async Task DeleteAsync(CancellationToken cancellationToken = defa private async Task TryReadAsAsync(HttpResponseMessage response, CancellationToken cancellationToken) { + if (response.StatusCode == HttpStatusCode.NoContent) return null; + try { return await response.Content.ReadAsAsync(Serializers, cancellationToken).NoContext(); diff --git a/src/TypedRest/Http/ResponseCache.cs b/src/TypedRest/Http/ResponseCache.cs index f02ab923..210c83e8 100644 --- a/src/TypedRest/Http/ResponseCache.cs +++ b/src/TypedRest/Http/ResponseCache.cs @@ -16,7 +16,7 @@ public sealed class ResponseCache /// /// The ; null if the response is not eligible for caching. public static ResponseCache? From(HttpResponseMessage response) - => response is {IsSuccessStatusCode: true, Headers.CacheControl: null or {NoStore: false}} + => response is {IsSuccessStatusCode: true, StatusCode: not HttpStatusCode.NoContent, Headers.CacheControl: null or {NoStore: false}} ? new(response) : null;