Log cURL from your HTTP request

Ugo Lattanzi
3 min readDec 12, 2022

--

I am a sports fan and this is meant to be a blog not exclusively technical but in this case we are talking about cURL and not Curling 😎.

Back to being serious, as developer I’m sure you’ve been in the situation many times where a problem has occurred and you need to have to replicate the call in order to debug it locally or write a test on it.

This is a pretty common scenario that could came from a quality team, a colleague or yourself.

Tester: I just opened a but …..
You: Do you have the cURL so I can replicate because it works on my machine!!!

That’s it, we all experienced these scenes and what definitely emerges from this story is the need … save the cURL.

But what is a cURL?

cURL means client url, is a command line tool that developers use to transfer data to and from a server. It supports different protocols like HTTP or HTTPs and it includes all the needed data to replicate the request like HEADER and Payload.

The way to use is pretty simple, just open the terminal and write the request

curl --request GET https://medium.com/@imperugo

and the terminal will print the content of the url (the HTML in this example).

The reason of why cURL is so important is that it doesn’t require any interface, is lightweight and run on every operating system.

Because of these characteristics over time it has become a tool for interchange between applications and today most of the important tools like Postman supports it.

cURL and Postman with love

Now that we know the history and the importance of having cURL let’s look at how we can use it with Postman (for those who don’t know it we are talking about one of the best tools for APIs development).

The way to do that is super easy.

Log cURL in an ASP.NET Core application

The last but super important step, we know the need, we know how to use the cURL but we don’t know how to log it automatically.

In order to do that I’ve create a NuGet Package called Imperugo.HttpRequestToCurl (Github repo here) and is available for all, for free, under MIT license (basically you can use the library for any purpose).

As with all NuGet libraries, this does not make a difference, so let’s move on to the installation:

dotnet add package Imperugo.HttpRequestToCurl

After that, everywhere we have an HttpRequest available (controller or minimal api doesn’t change) we can do just this:

public async Task<IActionResult> IndexAsync()
{
var curl = await Request.ToCurlAsync();

_logger.LogDebug(curl);
return View();
}

And that’s it! The log will print in the output the cURL

dbug: Imperugo.HttpRequestToCurl.Sample.Controllers.HomeController[0]
curl --location --request GET 'http://localhost:5069/'
--header 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8'
--header 'Connection: keep-alive'
--header 'Host: localhost:5069'
--header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36'
--header 'Accept-Encoding: gzip, deflate, br'
--header 'Accept-Language: en-GB,en'
--header 'Cache-Control: max-age=0'
--header 'Upgrade-Insecure-Requests: 1'
--header 'sec-ch-ua: "Brave";v="107", "Chromium";v="107", "Not=A?Brand";v="24"'
--header 'sec-ch-ua-mobile: ?0'
--header 'sec-ch-ua-platform: "macOS"'
--header 'Sec-GPC: 1'
--header 'Sec-Fetch-Site: none'
--header 'Sec-Fetch-Mode: navigate'
--header 'Sec-Fetch-User: ?1'
--header 'Sec-Fetch-Dest: document'

How cool is that?
The method offers some options like insecure flag or delimiters.

If you have reports, either of problems or new features, I’ll leave the link to the repository where you can open a report.

Github repo https://github.com/imperugo/HttpRequestToCurl

Happy coding 😎

--

--

Ugo Lattanzi

Web addicted, ASPInsider, unhappy Noder, Python lover and GoLang padawan. Doing crazy somewhere with someone. The bio is not true.