HttpClientToCurlGenerator icon indicating copy to clipboard operation
HttpClientToCurlGenerator copied to clipboard

Add Feature To Generate cURL directly from HttpRequestMessage

Open babaktaremi opened this issue 10 months ago • 3 comments

this feature will be quite handy to have . when you use DelegatingHandler , you can set a role or do something for entire services that use specific HttpClient. Currently I can mitigate it by injecting IHttpClientFactory like this:


using System.Text;
using Microsoft.Extensions.Logging;

namespace OnlineGateway.Infrastructure.ExternalApi.Common;

public class RequestCurlLoggerDelegatingHandler(ILogger<RequestCurlLoggerDelegatingHandler> logger,IHttpClientFactory httpClientFactory) : DelegatingHandler
{
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
        CancellationToken cancellationToken)
    {
        var response = await base.SendAsync(request, cancellationToken);

        var curl = HttpClientToCurl.Main.GenerateCurlInString(httpClientFactory.CreateClient(), request);
      
        logger.LogWarning("Generated Curl {curl} with response {response} and status code {statusCode}", curl,
            await response.Content.ReadAsStringAsync(cancellationToken), response.StatusCode.ToString("D"));

        return response;
    }
}

I don't like it. Can we have some mechanism to generate cURL directly from HttpRequestMessage

babaktaremi avatar Feb 25 '25 08:02 babaktaremi

Thanks, dear @babaktaremi. if I understand correctly, you can use the extension like this: string curlScript = _httpClient.GenerateCurlInString(httpRequestMessage); instead of this: var curl = HttpClientToCurl.Main.GenerateCurlInString(httpClientFactory.CreateClient(), request); In this way, you can use HttpRequestMessage directly. _httpClient object is an instance of IHttpClientFactory.

amingolmahalle avatar Feb 25 '25 08:02 amingolmahalle

Thanks, dear @babaktaremi. if I understand correctly, you can use the extension like this: string curlScript = _httpClient.GenerateCurlInString(httpRequestMessage); instead of this: var curl = HttpClientToCurl.Main.GenerateCurlInString(httpClientFactory.CreateClient(), request); In this way, you can use HttpRequestMessage directly. _httpClient object is an instance of IHttpClientFactory.

I know. It would be better that there was a method which had not any dependency on HttpClient since HttpRequestMessage has all the information needed to create the cURL

babaktaremi avatar Feb 25 '25 09:02 babaktaremi

I'll check it, @babaktaremi. If you have any other suggestions, please feel free to share them there!

amingolmahalle avatar Feb 25 '25 15:02 amingolmahalle

@amingolmahalle please assign me

mozhganEtaati avatar Jun 06 '25 15:06 mozhganEtaati

Thanks for the suggestion @babaktaremi — I completely agree that this feature is quite handy to have.

I've recently contributed to the project and added support for generating a CURL command directly from HttpRequestMessage , which has no dependency on HttpClient.

You can now use it like this:

string curlResult = httpRequestMessage.GenerateCurlInString(baseAddress);

This should make it much easier to use in different scenarios. The feature will be available in the next release (v2.0.7). Would love to hear your thoughts once you get a chance to try it out!

mozhganEtaati avatar Jul 10 '25 07:07 mozhganEtaati

Any idea when version 2.0.7 will be released?

babaktaremi avatar Aug 19 '25 08:08 babaktaremi

@babaktaremi Version 2.0.7 was released two days ago and is already available on NuGet 🎉

mozhganEtaati avatar Aug 23 '25 07:08 mozhganEtaati