Skip to content

Commit 57394ec

Browse files
Add Sample.InSpecific project
1 parent 41c024c commit 57394ec

File tree

7 files changed

+139
-0
lines changed

7 files changed

+139
-0
lines changed

HttpClientToCurlGenerator.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientToCurlTest", "tes
2929
EndProject
3030
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpRequestMessageToCurlTest", "tests\HttpRequestMessageToCurlTest\HttpRequestMessageToCurlTest.csproj", "{69E31075-F14E-1DE2-1D6E-D934A5C0480F}"
3131
EndProject
32+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HttpClientToCurl.Sample.InSpecific", "examples\HttpClientToCurl.Sample.InSpecific\HttpClientToCurl.Sample.InSpecific.csproj", "{9D56718F-C9E6-4C45-926D-97599072DA35}"
33+
EndProject
3234
Global
3335
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3436
Debug|Any CPU = Debug|Any CPU
@@ -63,6 +65,10 @@ Global
6365
{69E31075-F14E-1DE2-1D6E-D934A5C0480F}.Debug|Any CPU.Build.0 = Debug|Any CPU
6466
{69E31075-F14E-1DE2-1D6E-D934A5C0480F}.Release|Any CPU.ActiveCfg = Release|Any CPU
6567
{69E31075-F14E-1DE2-1D6E-D934A5C0480F}.Release|Any CPU.Build.0 = Release|Any CPU
68+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
69+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Debug|Any CPU.Build.0 = Debug|Any CPU
70+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Release|Any CPU.ActiveCfg = Release|Any CPU
71+
{9D56718F-C9E6-4C45-926D-97599072DA35}.Release|Any CPU.Build.0 = Release|Any CPU
6672
EndGlobalSection
6773
GlobalSection(SolutionProperties) = preSolution
6874
HideSolutionNode = FALSE
@@ -75,6 +81,7 @@ Global
7581
{5A8427BC-0821-E973-7221-263D13156248} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
7682
{8CC76F1F-5845-D81E-5E9A-113F913A444B} = {E36BF269-7F5D-4DE7-99B0-14567F9CD6B3}
7783
{69E31075-F14E-1DE2-1D6E-D934A5C0480F} = {E36BF269-7F5D-4DE7-99B0-14567F9CD6B3}
84+
{9D56718F-C9E6-4C45-926D-97599072DA35} = {A8574DB9-8411-4F81-A82E-F97AD00EF8AF}
7885
EndGlobalSection
7986
GlobalSection(ExtensibilityGlobals) = postSolution
8087
SolutionGuid = {E5E0FFF6-54C3-4BA1-91F3-EF3513A18D5D}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Text;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace HttpClientToCurl.Sample.InSpecific.Controllers;
5+
6+
[ApiController]
7+
[Route("[controller]")]
8+
public class MyController(IHttpClientFactory httpClientFactory) : ControllerBase
9+
{
10+
private readonly IHttpClientFactory _httpClientFactory = httpClientFactory;
11+
12+
[HttpGet]
13+
public async Task Send()
14+
{
15+
string apiUrl = "https://jsonplaceholder.typicode.com/posts";
16+
17+
try
18+
{
19+
// Create a sample JSON payload
20+
string jsonPayload =
21+
$"{{\"title\":\"New Post\",\"body\":\"This is the body of the new post\",\"userId\":1}}";
22+
23+
// Create HttpRequestMessage with the POST verb
24+
HttpRequestMessage request = new(HttpMethod.Post, apiUrl);
25+
26+
// Set up the request headers
27+
request.Headers.Add("Authorization", "Bearer YourAccessToken"); // Add any necessary headers
28+
29+
// Set the request content with the JSON payload
30+
request.Content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
31+
32+
// Log the curl command for debugging or testing.
33+
// This generates a curl command that can be imported into Postman.
34+
// Use it to check and compare against all the requirements.
35+
36+
// Send the request
37+
HttpResponseMessage response = await _httpClientFactory.CreateClient("my-client").SendAsync(request);
38+
39+
// Check if the request was successful (status code 200-299)
40+
if (response.IsSuccessStatusCode)
41+
{
42+
// Read and print the response content as a string
43+
string responseBody = await response.Content.ReadAsStringAsync();
44+
Console.WriteLine("Response from the API:\n" + responseBody);
45+
}
46+
else
47+
{
48+
Console.WriteLine($"Error: {response.StatusCode} - {response.ReasonPhrase}");
49+
}
50+
}
51+
catch (Exception ex)
52+
{
53+
Console.WriteLine($"Exception: {ex.Message}");
54+
}
55+
}
56+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net9.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\src\HttpClientToCurl\HttpClientToCurl.csproj" />
11+
</ItemGroup>
12+
13+
</Project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using HttpClientToCurl.Config.Others;
2+
using HttpClientToCurl.HttpMessageHandlers;
3+
using static HttpClientToCurl.Extensions.ServiceCollectionExtensions;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
builder.Services.AddControllers();
7+
builder.Services.AddHttpClientToCurl(config =>
8+
{
9+
config.ShowMode = ShowMode.Console | ShowMode.File;
10+
config.NeedAddDefaultHeaders = true;
11+
config.ConsoleEnableCodeBeautification = true;
12+
config.FileConfigPath = "C:\\Users\\Public";
13+
config.FileConfigFileName = "curl_commands";
14+
}, false);
15+
builder.Services.AddHttpClient("my-client")
16+
.AddHttpMessageHandler<CurlGeneratorHttpMessageHandler>();
17+
18+
var app = builder.Build();
19+
20+
app.UseHttpsRedirection();
21+
app.UseAuthorization();
22+
app.MapControllers();
23+
app.Run();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"http": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "http://localhost:5062",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development"
11+
}
12+
},
13+
"https": {
14+
"commandName": "Project",
15+
"dotnetRunMessages": true,
16+
"launchBrowser": false,
17+
"applicationUrl": "https://localhost:7001;http://localhost:5062",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
}
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)