Created
June 2, 2023 01:59
-
-
Save pedrovasconcellos/f797a1d475c53a46a659cc57438d69a3 to your computer and use it in GitHub Desktop.
get Curl From HttpClient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.