Refit with customized reasonphrase #1500
-
|
Hi, I'm using refit to call a Web API. My result object has an Error property which is populated whenever an Error occurs. How can I customize the ReasonPhrase of the Refit APIResponse to reflect the Error Property of my result object? Thanks for your help |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
ReasonPhrase is set by your server, not by Refit. Refit just surfaces whatever came back on the wire. Two parts:
httpContext.Features.Get<IHttpResponseFeature>()!.ReasonPhrase = result.Error;Note: ReasonPhrase only exists in HTTP/1.x. Over HTTP/2 and HTTP/3 it is gone, so do not rely on it for transporting error detail. Prefer putting the error in the response body (for example a ProblemDetails or your FluentResults payload) and returning a 4xx status.
[Post("/things")]
Task<ApiResponse<MyResult>> CreateThing([Body] MyRequest req);
var resp = await api.CreateThing(req);
if (!resp.IsSuccessStatusCode)
{
var reason = resp.ReasonPhrase; // HTTP/1.x only
var body = resp.Error?.Content; // the deserialized/raw error body - recommended
}Recommendation: carry the meaningful error in the body and deserialize it, rather than ReasonPhrase, since it is version-dependent and length/charset limited. |
Beta Was this translation helpful? Give feedback.
ReasonPhrase is set by your server, not by Refit. Refit just surfaces whatever came back on the wire.
Two parts:
Note: ReasonPhrase only exists in HTTP/1.x. Over HTTP/2 and HTTP/3 it is gone, so do not rely on it for transporting error detail. Prefer putting the error in the response body (for example a ProblemDetails or your FluentResults payload) and returning a 4xx status.
ApiResponse<T>(or catchApiException) and read it: