Skip to content

A simple extension method that generate curl command from HTTP request

License

Notifications You must be signed in to change notification settings

imperugo/HttpRequestToCurl

Repository files navigation

Imperugo.HttpRequestToCurl

Nuget Nuget GitHub

The idea of this library is to have a curl from an HttpRequest instance. The library produce a string that could be logged everywhere (console, db and so on).

Read the blog post here for more information.

Quickstart

Installation

Add the NuGet Package to your project:

dotnet add package Imperugo.HttpRequestToCurl

Usage

The useage is absolutely easy. Just add the reference and, anywhere in your code write this:

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using Imperugo.HttpRequestToCurl.Sample.Models;

namespace Imperugo.HttpRequestToCurl.Sample.Controllers;

public class HomeController : Controller
{
    private readonly ILogger<HomeController> _logger;

    public HomeController(ILogger<HomeController> logger)
    {
        _logger = logger;
    }

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

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

That code produces this output

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'

If you want to use the --insecure you could do it with the specific parameter.

var curl = await Request.ToCurlAsync(insecure:true);

By default the ToCurl command generates an output that is compatible with the Bash but, if you are running on Windows or another system and you need a different output, you can specify it as parameter

Right now the supported output are:

  • Bash (default)
  • Command
  • Powershell
var curl = await Request.ToCurlAsync(Extensions.ToCurlOptions.CmdExe);

Sample

Take a look here

License

Imperugo.HttpRequestToCurl MIT licensed.

Contributing

Thanks to all the people who already contributed!

About

A simple extension method that generate curl command from HTTP request

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages