From cdf26d87194fa8b46a9f5edecc711a25c69b6f9e Mon Sep 17 00:00:00 2001 From: nurdotnet <278048682+nurdotnet@users.noreply.github.com> Date: Tue, 21 Apr 2026 23:49:58 +0500 Subject: [PATCH] fix(moysklad): add User-Agent header + enable HTTP auto-decompression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two issues surfaced after the previous gzip-removal: 1. MoySklad's nginx edge returned 415 on some requests without a User-Agent. Send a friendly UA string (food-market/0.1 + repo URL). 2. Previous fix dropped gzip support entirely; re-enable it properly by configuring AutomaticDecompression on the typed HttpClient's primary handler via AddHttpClient.ConfigurePrimaryHttpMessageHandler. Now the response body is transparently decompressed before the JSON deserializer sees it — no more 0x1F errors. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/food-market.api/Program.cs | 8 ++++++-- .../Integrations/MoySklad/MoySkladClient.cs | 9 +++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) 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; }