C
C#9mo ago
cow

From HttpClient, can I save/copy the request to share with someone?

Is there a way to copy raw request details? For example, I'm looking for a similar output like: - in Bruno you right-click a request and click "Generate Code" - in Postman you click the "view code" button on a request Thanks
16 Replies
Angius
Angius9mo ago
No, HttpClient is neither Postman nor Bruno It's used to make HTTP requests to fetch or send some data Not to share those requests in a human-readable or reproductible format You can maybe code this functionality yourself, if you want
cow
cowOP9mo ago
i know HttpClient is neither Postman or Bruno, it was an example of what data I want
lycian
lycian9mo ago
The only way I can think of is to construct the HttpRequestMessage and use that to find info before using HttpClient to send https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httprequestmessage?view=net-8.0
HttpRequestMessage Class (System.Net.Http)
Represents a HTTP request message.
lycian
lycian9mo ago
I believe HttpClient disposes the request on send, so make sure you pull out info before you do that
cow
cowOP9mo ago
yeah ok, I thought maybe I was overlooking some helpful inbuilt function like .DumpRequest() or something
cow
cowOP9mo ago
Stack Overflow
How to get verbose info of the HTTP request from HttpClient like th...
I am writing a WinForms program (C#) using System.Net.Http.HttpClient to download webpages. I'd like to get verbose info of the HTTP(S) requests like the following from libcurl: * Trying xxx.xxx...
cow
cowOP9mo ago
void PrintHttpGetResponse(HttpResponseMessage response) {
Console.WriteLine($"Content Type ---> {response.Content.Headers.ContentType}");
Console.WriteLine($"Status Code ---> {response.StatusCode}");
Console.WriteLine($"Headers ---> {response.Headers}");
Console.WriteLine($"Connection ---> {response.Headers.Connection}");
Console.WriteLine($"IsSuccessStatusCode ---> {response.IsSuccessStatusCode}");
}
void PrintHttpGetResponse(HttpResponseMessage response) {
Console.WriteLine($"Content Type ---> {response.Content.Headers.ContentType}");
Console.WriteLine($"Status Code ---> {response.StatusCode}");
Console.WriteLine($"Headers ---> {response.Headers}");
Console.WriteLine($"Connection ---> {response.Headers.Connection}");
Console.WriteLine($"IsSuccessStatusCode ---> {response.IsSuccessStatusCode}");
}
lycian
lycian9mo ago
that gets the response, not the request
cow
cowOP9mo ago
oh woops didnt read it close enough
lycian
lycian9mo ago
but you basically can do the same with HttpRequestMessage
cow
cowOP9mo ago
ill give it a try, thanks tbh i really thought there would be a native function
Angius
Angius9mo ago
You're the first person I heard of that would use it lol
cow
cowOP9mo ago
really? i've had plenty of requests from support teams and such to share the raw request sometimes i use wireshark or fiddler to get it, but that's an annoying extra step I was trying to reduce
cow
cowOP9mo ago
GitHub
GitHub - amingolmahalle/HttpClientToCurlGenerator: A NuGet package ...
A NuGet package for generating curl script of HttpClient in .NET ( C# | CSharp | Dotnet ) supported features: Post, Get, Put, and Delete. content types: application/json, text/xml, application/x-w...
cow
cowOP9mo ago
cool
Henkypenky
Henkypenky9mo ago
you can intercept the request before it goes down the pipeline with a DelegatingHandler it will give you headers, verb, version, url, body etc something like this:
HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler()));

//ADD CODE BELOW - Use client



//ADD CODE ABOVE - Use client

public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("Request:");
Console.WriteLine(request.ToString());
if (request.Content != null)
{
Console.WriteLine(await request.Content.ReadAsStringAsync());
}
Console.WriteLine();

HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();

return response;
}
}
HttpClient client = new HttpClient(new LoggingHandler(new HttpClientHandler()));

//ADD CODE BELOW - Use client



//ADD CODE ABOVE - Use client

public class LoggingHandler : DelegatingHandler
{
public LoggingHandler(HttpMessageHandler innerHandler) : base(innerHandler)
{
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
Console.WriteLine("Request:");
Console.WriteLine(request.ToString());
if (request.Content != null)
{
Console.WriteLine(await request.Content.ReadAsStringAsync());
}
Console.WriteLine();

HttpResponseMessage response = await base.SendAsync(request, cancellationToken);

Console.WriteLine("Response:");
Console.WriteLine(response.ToString());
if (response.Content != null)
{
Console.WriteLine(await response.Content.ReadAsStringAsync());
}
Console.WriteLine();

return response;
}
}
Want results from more Discord servers?
Add your server