Skip to content

Commit d5dfdd5

Browse files
claudiamurialdoclaudiamurialdo
andauthored
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]>
1 parent c1c65d5 commit d5dfdd5

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
@@ -144,6 +144,7 @@
144144
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
145145
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.22.0" PrivateAssets="ALL" />
146146
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.1.0" PrivateAssets="All" />
147+
<PackageReference Include="MimeTypesMap" Version="1.0.9" />
147148
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.7.0" PrivateAssets="All" />
148149
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.7.0" PrivateAssets="All" />
149150
<PackageReference Include="Pgvector" Version="0.3.0" PrivateAssets="All" />

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;
@@ -721,7 +722,11 @@ private void StartMultipartFile(string name, string s)
721722
name = Path.GetFileNameWithoutExtension(s);
722723
}
723724
SendStream.Write(MultiPart.Boundarybytes, 0, MultiPart.Boundarybytes.Length);
724-
string header = string.Format(MultiPart.HeaderTemplate, name, s, MimeMapping.GetMimeMapping(s));
725+
#if NETCORE
726+
string header = string.Format(MultiPart.HeaderTemplate, name, s, MimeTypesMap.GetMimeType(s));
727+
#else
728+
string header = string.Format(MultiPart.HeaderTemplate, name, s, MimeHelper.GetMimeMapping(s));
729+
#endif
725730
byte[] headerbytes = Encoding.UTF8.GetBytes(header);
726731
SendStream.Write(headerbytes, 0, headerbytes.Length);
727732
}
@@ -1078,7 +1083,7 @@ async Task ReadResponseDataAsync()
10781083
}
10791084
}
10801085
}
1081-
#endif
1086+
#endif
10821087
bool UseOldHttpClient(string name)
10831088
{
10841089
if (Config.GetValueOf("useoldhttpclient", out string useOld) && useOld.StartsWith("y", StringComparison.OrdinalIgnoreCase))
@@ -2155,5 +2160,27 @@ public string Password
21552160
}
21562161

21572162
}
2163+
#if !NETCORE
2164+
internal static class MimeHelper
2165+
{
2166+
private static readonly Dictionary<string, string> CustomMap =
2167+
new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
2168+
{
2169+
{ ".json", "application/json" },
2170+
};
2171+
2172+
public static string GetMimeMapping(string fileName)
2173+
{
2174+
string ext = Path.GetExtension(fileName);
2175+
2176+
if (!string.IsNullOrEmpty(ext) &&
2177+
CustomMap.TryGetValue(ext, out string forcedMime))
2178+
{
2179+
return forcedMime;
2180+
}
2181+
return MimeMapping.GetMimeMapping(fileName);
2182+
}
2183+
}
2184+
#endif
21582185

21592186
}

0 commit comments

Comments
 (0)