From 96254b52cdc3759962d199a8763c589c0992e504 Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Wed, 16 Sep 2026 19:48:13 +0100 Subject: [PATCH] Supprime un faux message d'erreur --- src/PostIt/PostIt/Services/YavscApiClient.cs | 34 +++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/PostIt/PostIt/Services/YavscApiClient.cs b/src/PostIt/PostIt/Services/YavscApiClient.cs index 7e3187d8b..04dbce1fd 100644 --- a/src/PostIt/PostIt/Services/YavscApiClient.cs +++ b/src/PostIt/PostIt/Services/YavscApiClient.cs @@ -194,10 +194,7 @@ public class YavscApiClient : IYavscApiClient, IAsyncDisposable using var response = await SendAsync(method, path, body is null ? null : () => JsonContent.Create(body), ct).ConfigureAwait(false); await EnsureSuccessOrThrowAsync(response, ct).ConfigureAwait(false); - var stream = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false); - var dto = await JsonSerializer.DeserializeAsync(stream, - new JsonSerializerOptions { PropertyNameCaseInsensitive = true }, ct).ConfigureAwait(false); - return dto!; + return await ReadJsonAsync(response, ct).ConfigureAwait(false); } /// @@ -215,10 +212,31 @@ public class YavscApiClient : IYavscApiClient, IAsyncDisposable using var response = await SendAsync(method, path, contentFactory, ct).ConfigureAwait(false); await EnsureSuccessOrThrowAsync(response, ct).ConfigureAwait(false); - var stream = await response.Content.ReadAsStreamAsync(ct).ConfigureAwait(false); - var dto = await JsonSerializer.DeserializeAsync(stream, - new JsonSerializerOptions { PropertyNameCaseInsensitive = true }, ct).ConfigureAwait(false); - return dto!; + return await ReadJsonAsync(response, ct).ConfigureAwait(false); + } + + /// + /// Deserialise the response body as , + /// tolerating an empty body. PUT /blogspot/{id} answers + /// 204 No Content: without this guard, + /// JsonSerializer.DeserializeAsync throws + /// JsonException: The input does not contain any JSON + /// tokens and the VM surfaces a successful save as an + /// error. Callers that genuinely need a payload + /// (GetPostAsync, CreatePostAsync) are unaffected: + /// their endpoints always return a body. + /// + private static async Task ReadJsonAsync(HttpResponseMessage response, CancellationToken ct) + { + if (response.StatusCode == HttpStatusCode.NoContent) + return default!; + + var payload = await response.Content.ReadAsStringAsync(ct).ConfigureAwait(false); + if (string.IsNullOrWhiteSpace(payload)) + return default!; + + return JsonSerializer.Deserialize(payload, + new JsonSerializerOptions { PropertyNameCaseInsensitive = true })!; } ///