How to Use ApiResponse Without a Return Type? #1594
-
|
Hello. I have a simple scenario: Hit an endpoint that returns nothing, but be able to inspect the response code/etc. If I didn't care about the response I'd do something like: [Post("/foo")]
public Task Foo(RequestBody request, CancellationToken cancellationToken);And if the HTTP hit returned some response, I'd model it with [Post("/foo")]
public Task<ApiResponse<ResponseDTO>> Foo(RequestBody request, CancellationToken cancellationToken);However, what if there is no response? [Post("/foo")]
public Task<ApiResponse<string>> Foo(RequestBody request, CancellationToken cancellationToken); // the string is always empty, but users of this client won't know that!Is there a way to use |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Use the non-generic [Post("/foo")]
Task<IApiResponse> Foo(RequestBody request, CancellationToken cancellationToken);Then: using var response = await api.Foo(request, ct);
if (response.IsSuccessStatusCode) { ... }
var code = response.StatusCode;Notes:
|
Beta Was this translation helpful? Give feedback.
Use the non-generic
IApiResponseas the return type. It is public and gives you status code, headers, success flags and error without forcing a content type.Then:
Notes:
IApiResponse(no type parameter) is the public, content-less response wrapper. It is verified as a supported return type (see ResponseTests.cs which usesTask<IApiResponse>andValueTask<IApiResponse>).using) since it owns the underlying response.ApiResponse<string>w…