Skip to content

Commit 33fd815

Browse files
claudiamurialdoclaudiamurialdo
andcommitted
Improve MIME type handling in GxHttpClient (#1236)
Added MimeTypesMap v1.0.9 to GxClasses.csproj to provide consistent MIME type resolution in .NET Core. Added a lightweight MIME helper for .NET Framework to avoid introducing an additional dependency while ensuring correct .json resolution. Co-authored-by: claudiamurialdo <[email protected]> (cherry picked from commit d5dfdd5)
1 parent 6abd022 commit 33fd815

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

dotnet/src/dotnetcore/GxClasses/GxClasses.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@
160160
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
161161
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" PrivateAssets="ALL" />
162162
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.1.0" PrivateAssets="All" />
163+
<PackageReference Include="MimeTypesMap" Version="1.0.9" />
163164
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" PrivateAssets="All" />
164165
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" PrivateAssets="All" />
165166
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="8.0.0" />

dotnet/src/dotnetframework/GxClasses/Domain/GxHttpClient.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace GeneXus.Http.Client
2323
using GeneXus.Utils;
2424

2525
#if NETCORE
26+
using HeyRed.Mime;
2627
using Microsoft.AspNetCore.WebUtilities;
2728
#endif
2829
using Mime;
@@ -711,7 +712,11 @@ private void StartMultipartFile(string name, string s)
711712
name = Path.GetFileNameWithoutExtension(s);
712713
}
713714
SendStream.Write(MultiPart.Boundarybytes, 0, MultiPart.Boundarybytes.Length);
714-
string header = string.Format(MultiPart.HeaderTemplate, name, s, MimeMapping.GetMimeMapping(s));
715+
#if NETCORE
716+
string header = string.Format(MultiPart.HeaderTemplate, name, s, MimeTypesMap.GetMimeType(s));
717+
#else
718+
string header = string.Format(MultiPart.HeaderTemplate, name, s, MimeHelper.GetMimeMapping(s));
719+
#endif
715720
byte[] headerbytes = Encoding.UTF8.GetBytes(header);
716721
SendStream.Write(headerbytes, 0, headerbytes.Length);
717722
}
@@ -1068,7 +1073,7 @@ async Task ReadResponseDataAsync()
10681073
}
10691074
}
10701075
}
1071-
#endif
1076+
#endif
10721077
bool UseOldHttpClient(string name)
10731078
{
10741079
if (Config.GetValueOf("useoldhttpclient", out string useOld) && useOld.StartsWith("y", StringComparison.OrdinalIgnoreCase))
@@ -2131,5 +2136,27 @@ public string Password
21312136
}
21322137

21332138
}
2139+
#if !NETCORE
2140+
internal static class MimeHelper
2141+
{
2142+
private static readonly Dictionary<string, string> CustomMap =
2143+
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
2144+
{
2145+
{ ".json", "application/json" },
2146+
};
2147+
2148+
public static string GetMimeMapping(string fileName)
2149+
{
2150+
string ext = Path.GetExtension(fileName);
2151+
2152+
if (!string.IsNullOrEmpty(ext) &&
2153+
CustomMap.TryGetValue(ext, out string forcedMime))
2154+
{
2155+
return forcedMime;
2156+
}
2157+
return MimeMapping.GetMimeMapping(fileName);
2158+
}
2159+
}
2160+
#endif
21342161

21352162
}

0 commit comments

Comments
 (0)