This package generates curl by extending HttpClient of .NET with DelegatingHandler i.e. Custom Message Handler.
Any requests made with HttpClient
is sent through the client pipeline containing only one message handler named CurlDelegatingHandler
The curl is returned in the response header named outputCurl
.
To prevent sending request to the network (with default HttpClientHandler
),
the header CanSend: False
should be added to the request.
Currently supported HTTP verbs are as follows
GET
POST
PUT
DELETE
Test cases were inspired from Postman Code Generators.
using System.Text;
using CurlGenerator;
string url = "https://jsonplaceholder.typicode.com/posts";
string jsonPayload = @"{""title"": ""New Post"", ""body"": ""This is the body of the new post"", ""userId"": 1}";
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
var httpClient = new HttpClient(new CurlDelegatingHandler());
httpClient.DefaultRequestHeaders.Add(Settings.CanSend, "False");
var result = await httpClient.PostAsync(url, content);
string outputCurl = result.Headers.GetValues(Settings.OutputCurl).FirstOrDefault();
Console.WriteLine(outputCurl);
Output
curl -X POST 'https://jsonplaceholder.typicode.com/posts' -H 'Content-Type: application/json; charset=utf-8' -H 'Content-Length: 78' -d '{"title": "New Post", "body": "This is the body of the new post", "userId": 1}'
- Builder methods can be decoupled further
- Support more
Settings
like Postman does