Add Feature To Generate cURL directly from HttpRequestMessage
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
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.
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
I'll check it, @babaktaremi. If you have any other suggestions, please feel free to share them there!
@amingolmahalle please assign me
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!
Any idea when version 2.0.7 will be released?
@babaktaremi Version 2.0.7 was released two days ago and is already available on NuGet 🎉