Skip to content

Commit

Permalink
Merge pull request #39 from amingolmahalle/bugfix/HCG-38
Browse files Browse the repository at this point in the history
Fixed Content-Length header
  • Loading branch information
naeemaei authored Mar 12, 2024
2 parents 316c8d4 + 64b5ae8 commit a52f986
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
7 changes: 7 additions & 0 deletions src/HttpClientToCurl/Builder/Concrete/Common/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace HttpClientToCurl.Builder.Concrete.Common;

public static class Constants
{
internal const string ContentLength = "Content-Length";
internal const string FormUrlEncodedContentType = "application/x-www-form-urlencoded";
}
10 changes: 4 additions & 6 deletions src/HttpClientToCurl/Builder/Concrete/Common/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net;
using System.Text;
using System.Web;
using HttpClientToCurl.Utility;
Expand Down Expand Up @@ -129,7 +128,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC
if (needAddDefaultHeaders && httpClient.DefaultRequestHeaders.Any())
{
var defaultHeaders =
httpClient.DefaultRequestHeaders.Where(dh => dh.Key != HttpRequestHeader.ContentLength.ToString());
httpClient.DefaultRequestHeaders.Where(dh => dh.Key != Constants.ContentLength);
foreach (var header in defaultHeaders)
{
stringBuilder
Expand All @@ -144,7 +143,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC

if (httpRequestMessage.Headers.Any())
{
var headers = httpRequestMessage.Headers.Where(h => h.Key != HttpRequestHeader.ContentLength.ToString());
var headers = httpRequestMessage.Headers.Where(h => h.Key != Constants.ContentLength);
foreach (var header in headers)
{
stringBuilder
Expand All @@ -159,8 +158,7 @@ internal static StringBuilder AddHeaders(this StringBuilder stringBuilder, HttpC

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

if (contentType == "application/x-www-form-urlencoded")
if (contentType == Constants.FormUrlEncodedContentType)
{
stringBuilder.AddFormUrlEncodedContentBody(body);
}
Expand Down
4 changes: 2 additions & 2 deletions src/HttpClientToCurl/HttpClientToCurl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

<PropertyGroup>
<Authors>Amin Golmahalleh</Authors>
<Version>2.0.5</Version>
<PackageReleaseNotes>2.0.5</PackageReleaseNotes>
<Version>2.0.6</Version>
<PackageReleaseNotes>2.0.6</PackageReleaseNotes>
<TargetFramework>netstandard2.1</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void GenerateCurl_With_ContentLength_For_PostMethod()
var requestUri = "api/test";
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(requestBody, Encoding.UTF8, MediaTypeNames.Application.Json) };
httpRequestMessage.Headers.Add("Authorization", "Bearer f69406a4-6b62-4734-a8dc-158f0fc308ab");
httpRequestMessage.Headers.Add("ContentLength", "123");
httpRequestMessage.Content.Headers.Add("Content-Length", "123");

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

0 comments on commit a52f986

Please sign in to comment.