From ee1e241e8e9408432e0b932ce076f37cc562bdfe Mon Sep 17 00:00:00 2001 From: azures04 Date: Sun, 25 Jan 2026 19:50:59 +0100 Subject: [PATCH] Improve HttpHelper body handling and error reporting HttpHelper.FetchAsync now supports HttpContent as a body parameter, allowing for more flexible request content. In MojangAPI.UploadSkinAsync, error messages now include the response body for better debugging. --- src/main/core/utils/Http.cs | 8 +++++--- src/main/utils/MojangAPI.cs | 12 ++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/core/utils/Http.cs b/src/main/core/utils/Http.cs index 6556fae..e861ff0 100644 --- a/src/main/core/utils/Http.cs +++ b/src/main/core/utils/Http.cs @@ -10,8 +10,6 @@ public static class HttpHelper { public static async Task FetchAsync(string url, HttpMethod? method = null, object? body = null, Dictionary? headers = null) { var request = new HttpRequestMessage(method ?? HttpMethod.Get, url); - request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64)"); - if (headers != null) { foreach (var header in headers) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); @@ -19,13 +17,17 @@ public static class HttpHelper { } if (body != null) { - if (body is string s) { + if (body is HttpContent httpContent) { + request.Content = httpContent; + } else if (body is string s) { request.Content = new StringContent(s, System.Text.Encoding.UTF8, "application/json"); } else { request.Content = JsonContent.Create(body, options: LauncherConstants._jsonOptions); } } + request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64)"); + return await Http.SendAsync(request); } diff --git a/src/main/utils/MojangAPI.cs b/src/main/utils/MojangAPI.cs index 25cf69f..784486f 100644 --- a/src/main/utils/MojangAPI.cs +++ b/src/main/utils/MojangAPI.cs @@ -9,19 +9,27 @@ public static class MojangAPI { public static async Task UploadSkinAsync(string filePath, string variant, string token) { using var content = new MultipartFormDataContent(); + content.Add(new StringContent(variant), "variant"); byte[] fileBytes = await File.ReadAllBytesAsync(filePath); var fileContent = new ByteArrayContent(fileBytes); fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("image/png"); + content.Add(fileContent, "file", Path.GetFileName(filePath)); - var response = await HttpHelper.FetchAsync("https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/skins", method: HttpMethod.Post, body: content, headers: new() { { "Authorization", $"Bearer {token}" } } ); + var response = await HttpHelper.FetchAsync( + "https://yggdrasil.azures.fr/minecraftservices/minecraft/profile/skins", + method: HttpMethod.Post, + body: content, + headers: new() { { "Authorization", $"Bearer {token}" } } + ); if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { - throw new Exception($"Erreur serveur: {response.StatusCode}"); + string errorBody = await response.Content.ReadAsStringAsync(); + throw new Exception($"Erreur serveur ({response.StatusCode}): {errorBody}"); } }