How to create CURL from HttpClient

Hamed Naeemaei 20 Reputation points
2024-01-23T11:28:16.2966667+00:00

I have many API calls byHttpClient in c# and want to have a debug view of these requests. For example:

string requestBody = @"{""name"":""reza"",""referenceCode"":""10001000"",""price"":10000}";
string requestUri = "submit";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");

using var httpClient = new HttpClient();
httpClient.BaseAddress = new Uri("http://localhost:8080/api/v1");
await client.PostAsync(requestUri, httpRequest.Content);

Now, how can generate CURL of this request? or are there any tools to generate CURL from HttpClient or HttpRequestMessage ?

curl -X POST 'http://localhost:8080/api/v1/submit' -H 'Authorization: Bearer 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{""name"":""reza"",""referenceCode"":10001000,""price"":10000}'"

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,231 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,362 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ruikai Feng - MSFT 2,526 Reputation points Microsoft Vendor
    2024-01-24T02:08:30.9666667+00:00

    Hi,@Hamed Naeemaei, there's no build-in support for your requirement,you may try with a thrid-party tool,such as HttpClientToCurl Add this package to your project: User's image

    Call GenerateCurlInString,GenerateCurlInConsole,GenerateCurlInFile method based on your requirement

    string requestBody = @"{""name"":""reza"",""referenceCode"":""10001000"",""price"":10000}";
    string requestUri = "submit";
    var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
    httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
    httpRequestMessage.Headers.Add("Authorization", $"Bearer {Guid.NewGuid().ToString()}");
    using var httpClient = new HttpClient();
    httpClient.BaseAddress = new Uri("http://localhost:8080/api/v1");
    
    var curl = httpClient.GenerateCurlInString(httpRequestMessage);
    //show the curl in console
    httpClient.GenerateCurlInConsole(httpRequestMessage);
    
    
    

    Result: User's image

    User's image

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread. Best regards, Ruikai Feng

    2 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Amin Golmahalleh 5 Reputation points
    2024-01-24T07:13:42.9+00:00

    Thanks, dear @Ruikai Feng - MSFT for suggesting the HttpClientToCurl extension.

    exactly This extension will help you to see whatever is set in HttpClient in the form of a curl script.

    And you can check if that is the correct data for sending to an external service or not. also if you have an error, you can check the script find your problem, and fix that. so easily. Also, it is a new way and fast way to create or update a collection of Postman, when you haven't got a Postman collection for your desired external service. It's easy to use. just you should install the package on your project from the below address and use sample codes for how to call and work with extensions. The Nuget package address is: Nuget Address

    You have 3 ways to see script results:

    1: Put it in a string variable:

    string curlScript = httpClient.GenerateCurlInString(httpRequestMessage);

    2: Show to the IDE console:

    httpClient.GenerateCurlInConsole(httpRequestMessage);

    3: Write in a file:

    httpClient.GenerateCurlInFile(httpRequestMessage);

    Also, the HttpClientToCurl extension has attractive configs that you can see examples in GitHub Address below:

    HttpClientToCurlGenerator

    I hope developers enjoy this extension in your projects.

    1 person found this answer helpful.
    0 comments No comments