4

In my C# code running .NET 6 (Azure Function) I am sending an HttpRequestMessage using HttpClient. It doesn't work but it should work, so I want to get the raw request that I am sending, including the header, so I can compare with the documentation and see the differences.

In the past I have used Fiddler but it doesn't work for me now, probably because of some security settings on my laptop. So I am looking for a solution within the world of Visual Studio 2022 or .NET 6 where I can get the raw request out for troubleshooting purposes.

This question is not really about code, but here is my code anyway.

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://myendpoint.com/rest/something");

var apiToken = "AOU9FrasdgasdfagtHJNV";
request.Headers.Add("Authorization", "Basic " + apiToken);

var message = new
{
    sender = "Hey",
    message = "Hello world",
    recipients = new[] { new { id = 12345678} }
};

request.Content = new StringContent(JsonSerializer.Serialize(message), Encoding.UTF8, "application/json");
request.Headers.Add("Accept", "application/json, text/javascript");
HttpResponseMessage response = await httpClient.SendAsync(request);

When SendAsync is invoked, I wish to know what exactly is sent, both header and content.

Niels Brinch
  • 3,033
  • 9
  • 48
  • 75
  • The request doesn't work, right? What is the exact response code you're getting? Have you tried using ILogger to log the response? Also, HttpResponseMessage has a property of type HttpRequestMessage that gives you information about the request that led to that response. https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpresponsemessage.requestmessage?view=net-6.0#system-net-http-httpresponsemessage-requestmessage – Daniel Apr 28 '22 at 11:04
  • I get a response and I can easily read the response. It's 401. However, I need to log the _request_ in order to show whether I am calling correctly. – Niels Brinch Apr 28 '22 at 11:55
  • would the answer from this question solve what you are trying to get? https://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient – Andreas Andersson Apr 29 '22 at 08:02
  • If you need only log, you can follow this https://dev.to/nikolicbojan/log-httpclient-request-and-response-based-on-custom-conditions-in-net-core-412f – Den Apr 29 '22 at 12:06
  • @AndreasAndersson that does not work because I want to log the headers as well. And I want to log the actual headers sent and not an approximate recreation of the headers because there are things internally in HttpClient that may alter them. Or not. I’m not sure. – Niels Brinch Apr 30 '22 at 11:11
  • @Den that is good but can I be sure that it logs what is actually sent? It’s just looking at properties on the request and logging them. Is there a guarantee that it will not be altered somewhere down the line? – Niels Brinch Apr 30 '22 at 11:16
  • Probably you should use Bearer instead of Basic in authorization value: `request.Headers.Add("Authorization", "Bearer " + apiToken);` – Michael Ushakov May 04 '22 at 07:43

1 Answers1

3

If you cannot use any proxy solution (like Fiddler) then I can see 2 options. One is described in comments in your question to use DelegatingHandler. You can read more about this in documentation. What is interesting is that HttpClient supports logging out of the box which is described in this section https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-6.0#logging of the article which describes DelegatingHandlers

If you are worried that something will manipulate the outgoing request then you can implement option 2. This is to create temporary asp.net core application with .UseHttpLogging() middleware plugged in into pipeline as described here https://learn.microsoft.com/en-us/aspnet/core/fundamentals/http-logging/?view=aspnetcore-6.0 That way you will know exactly how your request looks like from application which is being requested point of view. Now if you will point your azure function to you temporary app - you should see what gets send

Hope it helps

jgasiorowski
  • 1,033
  • 10
  • 20