Skip to content

C# client 202 Accepted with empty body throws ApiException error #2186

@jonathan-maia

Description

@jonathan-maia

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:

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions