-
Notifications
You must be signed in to change notification settings - Fork 235
Description
Bug Report
Hey there, I ran into a problem with the C# SDK when handling 202 responses with empty body.
Version
Svix C# SDK version 1.85.0 (NuGet)
Description
I was trying to use the SvixClient.MessageAttempt.ResendAsync(appId, messageId, endpointId) method.
However, even when the Svix API responds with 202 Accepted, the C# SDK throws an exception during response handling.
In the code linked below, the SDK always attempts to deserialize the response body:
svix-webhooks/csharp/Svix/SvixHttpClient.cs
Lines 104 to 128 in ca54b59
| async Task<ApiResponse<T>> FilterResponseForErrors<T>( | |
| HttpResponseMessage response, | |
| CancellationToken cancellationToken | |
| ) | |
| { | |
| if (response.IsSuccessStatusCode) | |
| { | |
| if ((int)response.StatusCode == 204) | |
| { | |
| return new ApiResponse<T> | |
| { | |
| Data = (T)(object)true, | |
| StatusCode = response.StatusCode, | |
| }; | |
| } | |
| var responseContent = await response.Content.ReadAsStringAsync(cancellationToken); | |
| var data = | |
| JsonConvert.DeserializeObject<T>(responseContent) | |
| ?? throw new ApiException( | |
| (int)response.StatusCode, | |
| $"Failed to deserialize response body, status code: {(int)response.StatusCode}", | |
| responseContent, | |
| response.Headers | |
| ); |
In this scenario, responseContent is an empty string (""). As a result, deserialization returns null, causing an ApiException to be thrown even though the HTTP status code indicates success.
I'm not entirely sure what the correct behavior would be here. One option would be to treat the 202 Accepted code similarly to the 204 No Content code and ignore deserialization? Another approach, which I believe is the most correct and which I used as a workaround, would be to avoid throwing an exception when the status code represents a successful response, even if the response body is empty.
Thanks in advance!