Skip to content

Commit b3c1179

Browse files
Amin GolMahalehAmin GolMahaleh
authored andcommitted
fix error when post method request doesn't body
1 parent e6b4378 commit b3c1179

File tree

4 files changed

+105
-45
lines changed

4 files changed

+105
-45
lines changed

README.md

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ If you like this project, learn something or you are using it in your applicatio
3737

3838
## **How to use HttpClientToCurlGenerator**:
3939

40-
### Sample code for **Post Method** (it will be written in the **console**):
40+
### **Post Method** sample code (it will be written in the **console**):
4141
```
4242
string requestBody = @"{ ""name"" : ""amin"",""requestId"" : ""10001000"",""amount"":10000 }";
4343
string requestUri = "api/test";
@@ -61,7 +61,7 @@ If you like this project, learn something or you are using it in your applicatio
6161
// Call PostAsync => await client.PostAsync(requestUri, httpRequest.Content);
6262
```
6363

64-
### Sample code **Post Method** for FormUrlEncodedContent (it will be written in the **console**):
64+
### **Post Method** sample code for FormUrlEncodedContent (it will be written in the **console**):
6565
```
6666
string requestBody = @"{ ""name"" : ""justin"",""requestId"" : 10001026,""amount"":26000 }";
6767
string requestUri = "api/test";
@@ -89,7 +89,7 @@ If you like this project, learn something or you are using it in your applicatio
8989
// Call PostAsync => await client.PostAsync(requestUri, httpRequest.Content);
9090
```
9191

92-
### Sample code for **Post Method** without set the requestUri (it will be written in the **console**):
92+
### **Post Method** sample code without set the requestUri (it will be written in the **console**):
9393
```
9494
string requestBody = @"{ ""name"" : ""soozan"",""requestId"" : ""10001027"",""amount"":27000 }";
9595
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/test");
@@ -111,7 +111,39 @@ If you like this project, learn something or you are using it in your applicatio
111111
// Call PostAsync => await client.PostAsync(httpRequestMessage.RequestUri?.ToString(), httpRequest.Content);
112112
```
113113

114-
### Sample code for **Post Method** (it will be written in the **file**):
114+
### **Post Method** sample code for xml (it will be written in the **console**):
115+
116+
```
117+
public void GenerateCurl_For_PostMethod()
118+
{
119+
string requestBody = @"<?xml version = ""1.0"" encoding = ""UTF-8""?>
120+
<Order>
121+
<Id>12</Id>
122+
<name>Jason</name>
123+
<requestId>10001024</requestId>
124+
<amount>240000</amount>
125+
</Order>";
126+
127+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, "api/test");
128+
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
129+
httpRequestMessage.Headers.Add("Authorization", Guid.NewGuid().ToString());
130+
131+
using var httpClient = new HttpClient();
132+
httpClient.BaseAddress = new Uri("http://localhost:1213");
133+
134+
httpClient.GenerateCurlInConsole(
135+
httpRequestMessage,
136+
config: config =>
137+
{
138+
config.TurnOn = true;
139+
config.NeedAddDefaultHeaders = true;
140+
config.EnableCodeBeautification = false;
141+
});
142+
143+
// Call PostAsync => await client.PostAsync(requestUri, httpRequest.Content);
144+
```
145+
146+
### **Post Method** sample code (it will be written in the **file**):
115147

116148
If the path variable is null or empty, then the file is created in the **root project**.
117149

@@ -142,7 +174,7 @@ If the filename variable is null or empty, then the current date will be set for
142174
// Call PostAsync => await client.PostAsync(requestUri, httpRequest.Content);
143175
```
144176

145-
### Sample code for **Get Method** (it will be written in the **console**):
177+
### **Get Method** sample code (it will be written in the **console**):
146178
```
147179
string requestUri = "api/test";
148180
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
@@ -165,7 +197,7 @@ If the filename variable is null or empty, then the current date will be set for
165197
// Call GetAsync => await client.GetAsync(requestUri);
166198
```
167199

168-
### Sample code for **Get Method** (it will be written in the **file**):
200+
### **Get Method** sample code (it will be written in the **file**):
169201

170202
If the path variable is null or empty, then the file is created in the **root project**.
171203

@@ -195,7 +227,7 @@ If the filename variable is null or empty, then the current date will be set for
195227
// Call GetAsync => await client.GetAsync(requestUri);
196228
```
197229

198-
### Sample code for **Put Method** (it will be written in the **console**):
230+
### **Put Method** sample code (it will be written in the **console**):
199231
```
200232
string requestBody = @"{ ""name"" : ""jadi"",""requestId"" : ""10001003"",""amount"":30000 }";
201233
string requestUri = "api/test";
@@ -219,7 +251,7 @@ If the filename variable is null or empty, then the current date will be set for
219251
// Call PutAsync => await client.PutAsync(requestUri, httpRequest.Content);
220252
```
221253

222-
### Sample code for **Put Method** (it will be written in the **file**):
254+
### **Put Method** sample code (it will be written in the **file**):
223255

224256
If the path variable is null or empty, then the file is created in the **root project**.
225257

@@ -250,7 +282,7 @@ If the filename variable is null or empty, then the current date will be set for
250282
// Call PutAsync => await client.PutAsync(requestUri, httpRequest.Content);
251283
```
252284

253-
### Sample code for **Patch Method** (it will be written in the **console**):
285+
### **Patch Method** sample code (it will be written in the **console**):
254286
```
255287
string requestBody = @"{ ""name"" : ""hamed"",""requestId"" : ""10001005"",""amount"":50000 }";
256288
string requestUri = "api/test";
@@ -274,7 +306,7 @@ If the filename variable is null or empty, then the current date will be set for
274306
// Call PatchAsync => await client.PatchAsync(requestUri, httpRequest.Content);
275307
```
276308

277-
### Sample code for **Patch Method** (it will be written in the **file**):
309+
### **Patch Method** sample code (it will be written in the **file**):
278310

279311
If the path variable is null or empty, then the file is created in the **root project**.
280312

@@ -305,7 +337,7 @@ If the filename variable is null or empty, then the current date will be set for
305337
// Call PatchAsync => await client.PatchAsync(requestUri, httpRequest.Content);
306338
```
307339

308-
### Sample code for **Delete Method** (it will be written in the **console**):
340+
### **Delete Method** sample code (it will be written in the **console**):
309341
```
310342
int id = 12;
311343
string requestUri = $"api/test/{id}";
@@ -329,7 +361,7 @@ If the filename variable is null or empty, then the current date will be set for
329361
// Call DeleteAsync => await client.DeleteAsync(requestUri);
330362
```
331363

332-
### Sample code for **Delete Method** (it will be written in the **file**):
364+
### **Delete Method** sample code (it will be written in the **file**):
333365

334366
If the path variable is null or empty, then the file is created in the **root project**.
335367

@@ -360,4 +392,6 @@ If the filename variable is null or empty, then the current date will be set for
360392
// Call DeleteAsync => await client.DeleteAsync(requestUri);
361393
```
362394

363-
I Hope Enjoying this extension in your projects.
395+
**You can see more samples in UnitTest files in Project.**
396+
397+
I hope enjoying this extension in your projects.

src/HttpClientToCurl/Builder.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ internal static StringBuilder Initialize(HttpMethod httpMethod)
2222
.Append(httpMethod.Method);
2323
}
2424

25-
return stringBuilder
26-
.Append(' ');
25+
return stringBuilder.Append(' ');
2726
}
2827

2928
internal static StringBuilder AddAbsoluteUrl(this StringBuilder stringBuilder, string baseUrl, string requestUri)
@@ -100,10 +99,10 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
10099

101100
internal static StringBuilder AddBody(this StringBuilder stringBuilder, HttpContent content)
102101
{
103-
string contentType = content.Headers.ContentType?.MediaType;
104-
string body = content.ReadAsStringAsync().GetAwaiter().GetResult();
102+
string contentType = content?.Headers?.ContentType?.MediaType;
103+
string body = content?.ReadAsStringAsync().GetAwaiter().GetResult();
105104

106-
if (!_IsValidBody(body, contentType))
105+
if (content is not null && !_IsValidBody(body, contentType))
107106
throw new JsonException($"exception in parsing body {contentType}!");
108107

109108
if (contentType == "application/x-www-form-urlencoded")

src/HttpClientToCurl/HttpClientToCurl.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
<PropertyGroup>
44
<Authors>Amin Golmahalleh</Authors>
5-
<Version>1.8.5</Version>
6-
<PackageReleaseNotes>1.8.5</PackageReleaseNotes>
5+
<Version>1.8.6</Version>
6+
<PackageReleaseNotes>1.8.6</PackageReleaseNotes>
77
<TargetFramework>netstandard2.1</TargetFramework>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<Nullable>disable</Nullable>

src/HttpClientToCurlGeneratorTest/UnitTest/MediaTypes/Json/SuccessCurlGeneratorTests.cs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@ public class SuccessCurlGeneratorTests
1111
#region :: GenerateCurl_For_PostMethod ::
1212

1313
[Theory]
14-
public void GenerateCurl_With_QueryString_For_PostMethod()
14+
public void GenerateCurl_For_PostMethods()
1515
{
1616
// Arrange
17-
string requestBody = @"{ ""name"" : ""amin"",""requestId"" : 10001000,""amount"":10000 }";
18-
19-
var queryString = new Dictionary<string, string>()
20-
{
21-
{ "id", "12" }
22-
};
23-
var requestUri = QueryHelpers.AddQueryString("api/test", queryString);
17+
var requestUri = "api/test";
2418
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
25-
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json);
26-
httpRequestMessage.Headers.Add("Authorization", "4797c126-3f8a-454a-aff1-96c0220dae61");
19+
httpRequestMessage.Headers.Add("Authorization", "56bfa7a0-0541-4d71-9efc-8b28219ac31a");
2720

2821
using var httpClient = new HttpClient();
2922
httpClient.BaseAddress = new Uri("http://localhost:1213");
@@ -41,11 +34,11 @@ public void GenerateCurl_With_QueryString_For_PostMethod()
4134
Assert.That(script, Does.StartWith("curl -X POST"));
4235
Assert.That(script?.Trim(),
4336
Is.EqualTo(
44-
@"curl -X POST http://localhost:1213/api/test?id=12 -H 'Authorization: 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{ ""name"" : ""amin"",""requestId"" : 10001000,""amount"":10000 }'"));
37+
@"curl -X POST http://localhost:1213/api/test -H 'Authorization: 56bfa7a0-0541-4d71-9efc-8b28219ac31a' -d ''"));
4538
}
46-
39+
4740
[Theory]
48-
public void GenerateCurl_Without_QueryString_For_PostMethod()
41+
public void GenerateCurl_For_PostMethod()
4942
{
5043
// Arrange
5144
string requestBody = @"{ ""name"" : ""sara"",""requestId"" : 10001001,""amount"":20000 }";
@@ -74,6 +67,40 @@ public void GenerateCurl_Without_QueryString_For_PostMethod()
7467
@"curl -X POST http://localhost:1213/api/test -H 'Authorization: 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{ ""name"" : ""sara"",""requestId"" : 10001001,""amount"":20000 }'"));
7568
}
7669

70+
[Theory]
71+
public void GenerateCurl_With_QueryString_For_PostMethod()
72+
{
73+
// Arrange
74+
string requestBody = @"{ ""name"" : ""amin"",""requestId"" : 10001000,""amount"":10000 }";
75+
76+
var queryString = new Dictionary<string, string>()
77+
{
78+
{ "id", "12" }
79+
};
80+
var requestUri = QueryHelpers.AddQueryString("api/test", queryString);
81+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
82+
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json);
83+
httpRequestMessage.Headers.Add("Authorization", "4797c126-3f8a-454a-aff1-96c0220dae61");
84+
85+
using var httpClient = new HttpClient();
86+
httpClient.BaseAddress = new Uri("http://localhost:1213");
87+
88+
// Act
89+
string script = Generator.GenerateCurl(
90+
httpClient,
91+
httpRequestMessage,
92+
requestUri,
93+
true);
94+
95+
// Assert
96+
Assert.That(script, Is.Not.Null);
97+
Assert.That(script, Is.Not.Empty);
98+
Assert.That(script, Does.StartWith("curl -X POST"));
99+
Assert.That(script?.Trim(),
100+
Is.EqualTo(
101+
@"curl -X POST http://localhost:1213/api/test?id=12 -H 'Authorization: 4797c126-3f8a-454a-aff1-96c0220dae61' -H 'Content-Type: application/json; charset=utf-8' -d '{ ""name"" : ""amin"",""requestId"" : 10001000,""amount"":10000 }'"));
102+
}
103+
77104
[Theory]
78105
public void GenerateCurl_Without_RequestUri_For_PostMethod()
79106
{
@@ -142,14 +169,11 @@ public void GenerateCurl_UrlEncoded_For_PostMethod()
142169
#region :: GenerateCurl_For_GetMethod ::
143170

144171
[Theory]
145-
public void GenerateCurl_With_QueryString_For_GetMethod()
172+
public void GenerateCurl_For_GetMethod()
146173
{
147174
// Arrange
148-
var queryString = new Dictionary<string, string>()
149-
{
150-
{ "id", "12" }
151-
};
152-
var requestUri = QueryHelpers.AddQueryString("api/test", queryString);
175+
176+
var requestUri = "api/test";
153177
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
154178
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json);
155179
httpRequestMessage.Headers.Add("Authorization", "703438f3-16ad-4ba5-b923-8f72cd0f2db9");
@@ -169,15 +193,18 @@ public void GenerateCurl_With_QueryString_For_GetMethod()
169193
Assert.That(script, Is.Not.Empty);
170194
Assert.That(script, Does.StartWith("curl"));
171195
Assert.That(script?.Trim(),
172-
Is.EqualTo(@"curl http://localhost:1213/api/test?id=12 -H 'Authorization: 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
196+
Is.EqualTo(@"curl http://localhost:1213/api/test -H 'Authorization: 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
173197
}
174-
198+
175199
[Theory]
176-
public void GenerateCurl_Without_QueryString_For_GetMethod()
200+
public void GenerateCurl_With_QueryString_For_GetMethod()
177201
{
178202
// Arrange
179-
180-
var requestUri = "api/test";
203+
var queryString = new Dictionary<string, string>()
204+
{
205+
{ "id", "12" }
206+
};
207+
var requestUri = QueryHelpers.AddQueryString("api/test", queryString);
181208
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri);
182209
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, MediaTypeNames.Application.Json);
183210
httpRequestMessage.Headers.Add("Authorization", "703438f3-16ad-4ba5-b923-8f72cd0f2db9");
@@ -197,7 +224,7 @@ public void GenerateCurl_Without_QueryString_For_GetMethod()
197224
Assert.That(script, Is.Not.Empty);
198225
Assert.That(script, Does.StartWith("curl"));
199226
Assert.That(script?.Trim(),
200-
Is.EqualTo(@"curl http://localhost:1213/api/test -H 'Authorization: 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
227+
Is.EqualTo(@"curl http://localhost:1213/api/test?id=12 -H 'Authorization: 703438f3-16ad-4ba5-b923-8f72cd0f2db9' -H 'Content-Type: application/json; charset=utf-8'"));
201228
}
202229

203230
#endregion

0 commit comments

Comments
 (0)