Skip to content

Instantly share code, notes, and snippets.

@pedrovasconcellos
Created June 2, 2023 01:59
Show Gist options
  • Save pedrovasconcellos/f797a1d475c53a46a659cc57438d69a3 to your computer and use it in GitHub Desktop.
Save pedrovasconcellos/f797a1d475c53a46a659cc57438d69a3 to your computer and use it in GitHub Desktop.
get Curl From HttpClient
public void Print(HttpClient httpClient){
var jsonContent = await response.Content.ReadAsStringAsync();
Console.WriteLine($"curl -X {httpClient.HttpRequestMessage.Method} \"{url}\" \\\n" +
$"{GetHeadersAsString(httpClient.HttpRequestMessage.Headers)} \\\n" +
$"-d \"{jsonContent}\"");
}
static string GetHeadersAsString(HttpHeaders headers)
{
string headersString = string.Empty;
foreach (var header in headers)
{
headersString += $"-H \"{header.Key}: {string.Join(", ", header.Value)}\" ";
}
return headersString.TrimEnd();
}
@amingolmahalle
Copy link

amingolmahalle commented Mar 7, 2024

Hello, dear @pedrovasconcellos. I have written a package for getting the curl script of HttpClient called HttpClientToCurl you can see its sources in GitHub: HttpClientToCurlGenerator
It's so suitable for debugging the HttpClient.
I will be happy if you inform me if you have any feedback and your solution to improve the code.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment