How To Generate Curl Script of the HttpClient in .NET

In this article, we want to tell you how to generate the curl of the HttpClient in .NET by the HttpClientToCurlGenerator extension. This extension will help you see whatever is set in HttpClient as a curl script.

Sometimes we may want to connect with an external service provider but when calling we come across many errors and we should spend a lot of time resolving errors. It happened to me many times.

for example, the external service provider expects properties a request to be sent to it in the form of The Camel case but we send data to it in the form of The Pascal Case. after much investigation, we have realized this issue. Or it is possible we forget to send one Property or a Header or we made a mistake in filling in the value of a Header or Property and many issues like these, which we spent a lot of time resolving. This extension makes our work very easy.

Before sending data to the service provider, this extension can show us exactly what we send it. (in the form of a curl in the variable, console, or the file).

After getting the curl, we can now call it in the terminal or import it in the Postman and check with the documents provided by the provider.

How to import Curl in Postman?

Open the Postman -> click on the Import button -> select the Raw text tab -> paste the curl script here -> then press the Continue button -> at the end press the button import.

Another use for this extension is when we don’t have any documents or Postman Collection from the external service provider. We can with curl scripts that have been created, create a Postman Collection for ourselves and share them with teammates.

You can see how simple and convenient it is!

Moreover, you can call this extension with just one line of code.

Using this extension is easy and simple. Just you should install the package on your project from the below address: Nuget Address

How to see the Script result?

You have 3 ways to see script results.

1. Show in the console

Examples and explanations about more configurations.

httpClient.GenerateCurlInConsole(httpRequestMessage, null);

Notice: when the curl script was written in the console, your IDE console may apply WordWrap automatically. you should remove enters from the script.

Parameters of the config are optional.

How to use this extension along with configurations?

httpClient.GenerateCurlInConsole(
 httpRequestMessage,
 config =>
 {
 config.TurnOn = true;
 config.NeedAddDefaultHeaders = true;
 config.EnableCodeBeautification = false;
 });
  • TurnOn = enable or disable the extension.(default is set to true )
  • NeedAddDefaultHeaders = add default headers to the script.(default is set to true )
  • EnableCodeBeautification = Script coloring for each HttpMethod.(default is set to false )

2- Write in a file

Examples and explanations about more configurations.

httpClient.GenerateCurlInFile(httpRequestMessage, null);

Parameters of ‘config’ are optional.

httpClient.GenerateCurlInFile(
 httpRequestMessage,
 config =>
 {
 config.Filename = “your filename”
 config.Path = “your path”
 config.TurnOn = true;
 config.NeedAddDefaultHeaders = true;
 });
  • TurnOn = enable or disable the extension.(default is set to true )
  • NeedAddDefaultHeaders = add default headers to the script.(default is set to true )
  • Filename = set specific Filename.(default is set to the current date.
  • e.g. 20220910.curl )
  • Path = set a specific Path.(default stored in ProjectDirectory\bin\Debug\netX )

3- Put into a variable

string script = httpClient.GenerateCurlInString(httpRequestMessage, null);

This is a complete example of when you want to send a request.

string requestBody = @"{""name"":""amin"",""requestId"":""10001000"",""amount"":10000}";
string requestUri = "api/test";
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:1213/v1/");

httpClient.GenerateCurlInConsole(
    httpRequestMessage,
    config =>
    {
        config.TurnOn = true;
        config.NeedAddDefaultHeaders = true;
        config.EnableCodeBeautification = false;
    });

 // Call PostAsync => await client.PostAsync(requestUri, httpRequest.Content);

Link to the project’s GitHub address to see the source of the project and see more examples: GitHub Address

I will be happy if you inform me if you have any feedback and your solution to improve the code and also if you find a problem. also, Please let me know if you contribute to the implementation and improvement of the project.

I hope you enjoy this extension in your projects.

Good luck!