I am trying to get some information from my response when handling errors, but I cannot seem to figure out why the response is never included. I have tried both AfterCall and OnError, both do not contain the response.
Request code:
return await ResponseDeserializer.DeserializeResponse<Account>(
await userAccount.GetStreamAsync(HttpCompletionOption.ResponseContentRead, ct),
"value",
ct);
As you can see, I have specified HttpCompletionOption.ResponseContentRead which, as far as I understand, means Flurl considers a request as "complete" only when it has read the response its content.
My current builder code for configuring requests:
request.BeforeCall(call => { call.LogStartCall(); });
request.OnError(call =>
{
call.LogIfFailed();
});
request.AfterCall(call =>
{
if (expectSystemMessage)
{
call.TryFindSystemMessage();
}
});
return request;
And finally, a short snippet of the code for LogIfFailed():
Url requestUrl = call.Request.Url;
var options = new Dictionary<string, string>()
{
{ "json", call.RequestBody },
{ "query", call.Request.Url.Query },
{ "headers", string.Join(" , ", call.Request.Headers.Where(tuple => !tuple.Name.Equals("Authorization"))) },
};
LogContext.PushProperty("method", call.Request.Verb.Method);
LogContext.PushProperty("base_uri", call.Request.Url.Root);
LogContext.PushProperty("uri", call.Request.Url.Path);
LogContext.PushProperty("options", options);
if (call.Response is null)
{
Log.Error("API request failed");
return;
}
LogContext.PushProperty("duration", call.Duration.Value.Milliseconds.ToString());
LogContext.PushProperty("response", await call.Response.GetStringAsync());
LogContext.PushProperty("status_code", call.Response.StatusCode.ToString());
if (!call.Response.Headers.TryGetFirst("SysMessages", out string? message))
{
Log.Error("API request failed");
return;
}
I read AfterCall and OnError can handle both synchronous as well as asynchronous methods, and LogIfFailed() is an asynchronous method.
If it helps, I am on FlurlHttp version 3.2.2!
How can I get Flurl to always include a response when available?
Thanks in advance!