Troubleshoot back-channel HTTP calls from the ASP.NET and C# Proxy example
Add a new class file to your project, name it RekClientDelegatingHandler.cs:
using System.Text;
..
public class RekClientDelegatingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
// Log all outgoing request HTTP headers
foreach (var header in request.Headers)
{
Console.WriteLine($"{header.Key}: {string.Join(' ', header.Value)}");
}
if (request.Content != null)
{
foreach (var header in request.Content.Headers)
{
Console.WriteLine($"{header.Key}: {string.Join(' ', header.Value)}");
}
}
// Write the full request as a curl command to console
var curlCommand = new StringBuilder();
curlCommand.Append("curl");
curlCommand.Append($" -X {request.Method}");
curlCommand.Append($" '{request.RequestUri}'");
foreach (var header in request.Headers)
{
curlCommand.Append($" -H '{header.Key}: {string.Join(' ', header.Value).Replace("'", "'\\''")}'");
}
if (request.Content != null)
{
foreach (var header in request.Content.Headers)
{
curlCommand.Append($" -H '{header.Key}: {string.Join(' ', header.Value).Replace("'", "'\\''")}'");
}
var content = await request.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false);
if (!string.IsNullOrEmpty(content))
{
curlCommand.Append($" --data '{content.Replace("'", "'\\''")}'");
}
}
Console.WriteLine(curlCommand.ToString());
return await base.SendAsync(request, cancellationToken).ConfigureAwait(false);
}
}Add this line to Startup.cs and ConfigureServices(IServiceCollection services):
services.AddTransient<RekClientDelegatingHandler>();Then for the clients you need to log more details for:
// Append to the AddHttpClient() registration
.AddHttpMessageHandler<RekClientDelegatingHandler>();Console should now show information HTTP headers that are sent, one per line, and a full translation to a curl command that you or our support can look at.