Skip to content

Commit 2768232

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

File tree

4 files changed

+81
-19
lines changed

4 files changed

+81
-19
lines changed

src/HttpClientToCurl/Builder.cs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Net;
2-
using System.Net.Mime;
32
using System.Text;
43
using System.Web;
54
using HttpClientToCurl.Utility;
@@ -51,7 +50,8 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
5150
bool hasHeader = false;
5251
if (needAddDefaultHeaders && httpClient.DefaultRequestHeaders.Any())
5352
{
54-
foreach (var header in httpClient.DefaultRequestHeaders.Where(dh => dh.Key != HttpRequestHeader.ContentLength.ToString()))
53+
var defaultHeaders = httpClient.DefaultRequestHeaders.Where(dh => dh.Key != HttpRequestHeader.ContentLength.ToString());
54+
foreach (var header in defaultHeaders)
5555
{
5656
stringBuilder
5757
.Append("-H")
@@ -65,7 +65,8 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
6565

6666
if (httpRequestMessage.Headers.Any())
6767
{
68-
foreach (var header in httpRequestMessage.Headers.Where(h => h.Key != HttpRequestHeader.ContentLength.ToString()))
68+
var headers = httpRequestMessage.Headers.Where(h => h.Key != HttpRequestHeader.ContentLength.ToString());
69+
foreach (var header in headers)
6970
{
7071
stringBuilder
7172
.Append("-H")
@@ -102,7 +103,7 @@ internal static StringBuilder AddBody(this StringBuilder stringBuilder, HttpCont
102103
string contentType = content?.Headers?.ContentType?.MediaType;
103104
string body = content?.ReadAsStringAsync().GetAwaiter().GetResult();
104105

105-
if (content is not null && !_IsValidBody(body, contentType))
106+
if (content is not null && !string.IsNullOrWhiteSpace(body) && !Helpers.IsValidBody(body, contentType))
106107
throw new JsonException($"exception in parsing body {contentType}!");
107108

108109
if (contentType == "application/x-www-form-urlencoded")
@@ -134,16 +135,4 @@ private static void _AppendBodyItem(StringBuilder stringBuilder, object body)
134135
.Append(body)
135136
.Append('\'')
136137
.Append(' ');
137-
138-
private static bool _IsValidBody(string body, string contentType)
139-
{
140-
switch (contentType)
141-
{
142-
case MediaTypeNames.Application.Json when body.IsValidJson() == false:
143-
case MediaTypeNames.Text.Xml when body.IsValidXml() == false:
144-
return false;
145-
default:
146-
return true;
147-
}
148-
}
149138
}

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.6</Version>
6-
<PackageReleaseNotes>1.8.6</PackageReleaseNotes>
5+
<Version>1.8.7</Version>
6+
<PackageReleaseNotes>1.8.7</PackageReleaseNotes>
77
<TargetFramework>netstandard2.1</TargetFramework>
88
<ImplicitUsings>enable</ImplicitUsings>
99
<Nullable>disable</Nullable>

src/HttpClientToCurl/Utility/Helpers.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Net.Http.Headers;
2+
using System.Net.Mime;
23

34
namespace HttpClientToCurl.Utility;
45

@@ -54,4 +55,16 @@ public static HttpRequestMessage FillHttpRequestMessage(HttpMethod httpMethod, H
5455

5556
return httpRequestMessage;
5657
}
58+
59+
public static bool IsValidBody(string body, string contentType)
60+
{
61+
switch (contentType)
62+
{
63+
case MediaTypeNames.Application.Json when body.IsValidJson() == false:
64+
case MediaTypeNames.Text.Xml when body.IsValidXml() == false:
65+
return false;
66+
default:
67+
return true;
68+
}
69+
}
5770
}

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,40 @@ public class SuccessCurlGeneratorTests
1111
#region :: GenerateCurl_For_PostMethod ::
1212

1313
[Theory]
14-
public void GenerateCurl_For_PostMethods()
14+
public void GenerateCurl_Without_Body_For_PostMethod()
1515
{
1616
// Arrange
1717
var requestUri = "api/test";
1818
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
19+
httpRequestMessage.Content = new StringContent(string.Empty, Encoding.UTF8, "application/json");
20+
httpRequestMessage.Headers.Add("Authorization", "c332e9a1-1e0e-44c2-b819-b0e7e8ff7d45");
21+
22+
using var httpClient = new HttpClient();
23+
httpClient.BaseAddress = new Uri("http://localhost:1213");
24+
25+
// Act
26+
string script = Generator.GenerateCurl(
27+
httpClient,
28+
httpRequestMessage,
29+
requestUri,
30+
true);
31+
32+
// Assert
33+
Assert.That(script, Is.Not.Null);
34+
Assert.That(script, Is.Not.Empty);
35+
Assert.That(script, Does.StartWith("curl -X POST"));
36+
Assert.That(script?.Trim(),
37+
Is.EqualTo(
38+
@"curl -X POST http://localhost:1213/api/test -H 'Authorization: c332e9a1-1e0e-44c2-b819-b0e7e8ff7d45' -H 'Content-Type: application/json; charset=utf-8' -d ''"));
39+
}
40+
41+
[Theory]
42+
public void GenerateCurl_Without_Content_For_PostMethod()
43+
{
44+
// Arrange
45+
var requestUri = "api/test";
46+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
47+
httpRequestMessage.Content = null;
1948
httpRequestMessage.Headers.Add("Authorization", "56bfa7a0-0541-4d71-9efc-8b28219ac31a");
2049

2150
using var httpClient = new HttpClient();
@@ -67,6 +96,37 @@ public void GenerateCurl_For_PostMethod()
6796
@"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 }'"));
6897
}
6998

99+
[Theory]
100+
public void GenerateCurl_With_ContentLength_For_PostMethod()
101+
{
102+
// Arrange
103+
string requestBody = @"{ ""name"" : ""sara"",""requestId"" : 10001001,""amount"":20000 }";
104+
105+
var requestUri = "api/test";
106+
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri);
107+
httpRequestMessage.Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json);
108+
httpRequestMessage.Headers.Add("Authorization", "f69406a4-6b62-4734-a8dc-158f0fc308ab");
109+
httpRequestMessage.Headers.Add("ContentLength", "123");
110+
111+
using var httpClient = new HttpClient();
112+
httpClient.BaseAddress = new Uri("http://localhost:1213");
113+
114+
// Act
115+
string script = Generator.GenerateCurl(
116+
httpClient,
117+
httpRequestMessage,
118+
requestUri,
119+
true);
120+
121+
// Assert
122+
Assert.That(script, Is.Not.Null);
123+
Assert.That(script, Is.Not.Empty);
124+
Assert.That(script, Does.StartWith("curl -X POST"));
125+
Assert.That(script?.Trim(),
126+
Is.EqualTo(
127+
@"curl -X POST http://localhost:1213/api/test -H 'Authorization: f69406a4-6b62-4734-a8dc-158f0fc308ab' -H 'Content-Type: application/json; charset=utf-8' -d '{ ""name"" : ""sara"",""requestId"" : 10001001,""amount"":20000 }'"));
128+
}
129+
70130
[Theory]
71131
public void GenerateCurl_With_QueryString_For_PostMethod()
72132
{

0 commit comments

Comments
 (0)