DEV Community

Amin Golmahalleh
Amin Golmahalleh

Posted on

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 in 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 Package Address:

https://www.nuget.org/packages/HttpClientToCurl

You have 3 ways to see script results:

1- Write in the console

Examples and explanations about more configurations:

httpClient.GenerateCurlInConsole(httpRequestMessage, null);
Enter fullscreen mode Exit fullscreen mode

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 requestUri and config are optional.

How to use this extension along with configurations:

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

TurnOn = enable or disable extension.(default is set to true )
NeedAddDefaultHeaders = add default headers to 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);
Enter fullscreen mode Exit fullscreen mode

Parameters of ‘requestUri’ and ‘config’ are optional.

httpClient.GenerateCurlInFile(
 httpRequestMessage,
 config =>
 {
 config.Filename = your filename
 config.Path = your path
 config.TurnOn = true;
 config.NeedAddDefaultHeaders = true;
 });
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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

https://github.com/amingolmahalle/HttpClientToCurlGenerator

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

Gmail Address:

amin.golmahalle@gmail.com

I hope you enjoy this extension in your projects.

Good luck!

Top comments (0)