diff --git a/src/food-market.api/Program.cs b/src/food-market.api/Program.cs index d353fb7..380b042 100644 --- a/src/food-market.api/Program.cs +++ b/src/food-market.api/Program.cs @@ -123,8 +123,12 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); - // MoySklad import integration - builder.Services.AddHttpClient(); + // MoySklad import integration. Auto-decompress gzip responses from MoySklad's edge. + builder.Services.AddHttpClient() + .ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler + { + AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate, + }); builder.Services.AddScoped(); builder.Services.AddHostedService(); diff --git a/src/food-market.infrastructure/Integrations/MoySklad/MoySkladClient.cs b/src/food-market.infrastructure/Integrations/MoySklad/MoySkladClient.cs index 3c1c394..b5497ea 100644 --- a/src/food-market.infrastructure/Integrations/MoySklad/MoySkladClient.cs +++ b/src/food-market.infrastructure/Integrations/MoySklad/MoySkladClient.cs @@ -37,8 +37,13 @@ private HttpRequestMessage Build(HttpMethod method, string pathAndQuery, string // after ';'). The typed MediaTypeWithQualityHeaderValue API normalizes to // "application/json; charset=utf-8" which MoySklad rejects with code 1062. req.Headers.TryAddWithoutValidation("Accept", "application/json;charset=utf-8"); - // Don't advertise gzip — HttpClient here isn't configured with AutomaticDecompression, - // and the JSON deserializer would choke on the gzip magic byte (0x1F). + // MoySklad's nginx edge returns 415 for requests without a User-Agent, and we want + // auto-decompression (Accept-Encoding is added automatically by HttpClient when + // AutomaticDecompression is set on the primary handler — see Program.cs). + if (!req.Headers.UserAgent.Any()) + { + req.Headers.TryAddWithoutValidation("User-Agent", "food-market/0.1 (+https://github.com/nurdotnet/food-market)"); + } return req; }