Skip to content

Commit a52f986

Browse files
authored
Merge pull request #39 from amingolmahalle/bugfix/HCG-38
Fixed Content-Length header
2 parents 316c8d4 + 64b5ae8 commit a52f986

File tree

4 files changed

+14
-9
lines changed

4 files changed

+14
-9
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace HttpClientToCurl.Builder.Concrete.Common;
2+
3+
public static class Constants
4+
{
5+
internal const string ContentLength = "Content-Length";
6+
internal const string FormUrlEncodedContentType = "application/x-www-form-urlencoded";
7+
}

src/HttpClientToCurl/Builder/Concrete/Common/Extensions.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Net;
21
using System.Text;
32
using System.Web;
43
using HttpClientToCurl.Utility;
@@ -129,7 +128,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
129128
if (needAddDefaultHeaders && httpClient.DefaultRequestHeaders.Any())
130129
{
131130
var defaultHeaders =
132-
httpClient.DefaultRequestHeaders.Where(dh => dh.Key != HttpRequestHeader.ContentLength.ToString());
131+
httpClient.DefaultRequestHeaders.Where(dh => dh.Key != Constants.ContentLength);
133132
foreach (var header in defaultHeaders)
134133
{
135134
stringBuilder
@@ -144,7 +143,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
144143

145144
if (httpRequestMessage.Headers.Any())
146145
{
147-
var headers = httpRequestMessage.Headers.Where(h => h.Key != HttpRequestHeader.ContentLength.ToString());
146+
var headers = httpRequestMessage.Headers.Where(h => h.Key != Constants.ContentLength);
148147
foreach (var header in headers)
149148
{
150149
stringBuilder
@@ -159,8 +158,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
159158

160159
if (httpRequestMessage.Content is not null && httpRequestMessage.Content.Headers.Any())
161160
{
162-
foreach (var header in httpRequestMessage.Content.Headers.Where(h =>
163-
h.Key != HttpRequestHeader.ContentLength.ToString()))
161+
foreach (var header in httpRequestMessage.Content.Headers.Where(h => h.Key != Constants.ContentLength))
164162
{
165163
stringBuilder
166164
.Append("-H")
@@ -185,7 +183,7 @@ internal static StringBuilder AddBody(this StringBuilder stringBuilder, HttpCont
185183
string contentType = content?.Headers?.ContentType?.MediaType;
186184
string body = content?.ReadAsStringAsync().GetAwaiter().GetResult();
187185

188-
if (contentType == "application/x-www-form-urlencoded")
186+
if (contentType == Constants.FormUrlEncodedContentType)
189187
{
190188
stringBuilder.AddFormUrlEncodedContentBody(body);
191189
}

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void GenerateCurl_With_ContentLength_For_PostMethod()
4545
var requestUri = "api/test";
4646
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) };
4747
httpRequestMessage.Headers.Add("Authorization", "Bearer f69406a4-6b62-4734-a8dc-158f0fc308ab");
48-
httpRequestMessage.Headers.Add("ContentLength", "123");
48+
httpRequestMessage.Content.Headers.Add("Content-Length", "123");
4949

5050
using var httpClient = new HttpClient();
5151
httpClient.BaseAddress = new Uri("http://localhost:1213/v1/");

0 commit comments

Comments
 (0)