From 42c5caff3a5f6ba6103265950a0047d97fff7473 Mon Sep 17 00:00:00 2001 From: Julian Rich Date: Fri, 5 Jun 2026 15:41:17 +1000 Subject: [PATCH 1/2] Improve the error output --- APSIM.POStats.Shared/WebUtilities.cs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/APSIM.POStats.Shared/WebUtilities.cs b/APSIM.POStats.Shared/WebUtilities.cs index 042ed9e..796f12a 100644 --- a/APSIM.POStats.Shared/WebUtilities.cs +++ b/APSIM.POStats.Shared/WebUtilities.cs @@ -4,6 +4,7 @@ using System.Net.Http.Headers; using System.Text; using System.Text.Json; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace APSIM.POStats.Shared @@ -49,7 +50,8 @@ private static async Task RequestAsync(RequestType type, string requestU response = await httpClient.PostAsync(requestUrl, new StringContent(jsonString, Encoding.UTF8, "application/json")); } - string body = await response.Content.ReadAsStringAsync(); + string fullResponse = await response.Content.ReadAsStringAsync(); + string body = GetHtmlBodyContent(fullResponse); Console.WriteLine($"Status: {(int)response.StatusCode} {response.StatusCode}"); if (response.StatusCode >= HttpStatusCode.BadRequest) @@ -61,8 +63,8 @@ private static async Task RequestAsync(RequestType type, string requestU output += $"Request:\n{response}\n"; output += $"Response:\n{body}\n"; - Console.WriteLine($"Error sending POST Request\n{output}"); - throw new Exception($"Error sending POST Request\n{output}"); + Console.WriteLine($"Error sending {type} for URL {requestUrl} Request\n{output}"); + throw new Exception($"Error sending {type} for URL {requestUrl} Request\n{output}"); } return body; } @@ -82,5 +84,23 @@ public static async Task PostAsync(string requestUrl, T jsonObject, s { return await RequestAsync(RequestType.POST, requestUrl, JsonSerializer.Serialize(jsonObject), authorizationToken); } + + /// + /// Extract the content of the body tag from an HTML response. If no body tag is found, return the original content. + /// + /// The HTML content to extract the body from. + /// The content of the body tag, or the original content if no body tag is found. + private static string GetHtmlBodyContent(string content) + { + if (string.IsNullOrEmpty(content)) + return content; + + Match bodyMatch = Regex.Match(content, @"]*>([\s\S]*?)", RegexOptions.IgnoreCase); + if (bodyMatch.Success) + { + return bodyMatch.Groups[1].Value; + } + else return content; + } } } \ No newline at end of file From a52332c1446e7a25e005cfe2e582f0bdd6ca11f1 Mon Sep 17 00:00:00 2001 From: Julian Rich <123442863+ric394@users.noreply.github.com> Date: Fri, 5 Jun 2026 17:33:50 +1000 Subject: [PATCH 2/2] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- APSIM.POStats.Shared/WebUtilities.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/APSIM.POStats.Shared/WebUtilities.cs b/APSIM.POStats.Shared/WebUtilities.cs index 796f12a..8457794 100644 --- a/APSIM.POStats.Shared/WebUtilities.cs +++ b/APSIM.POStats.Shared/WebUtilities.cs @@ -95,12 +95,19 @@ private static string GetHtmlBodyContent(string content) if (string.IsNullOrEmpty(content)) return content; + // Avoid regex work for non-HTML responses (e.g. JSON). + if (content.IndexOf("]*>([\s\S]*?)", RegexOptions.IgnoreCase); - if (bodyMatch.Success) - { - return bodyMatch.Groups[1].Value; - } - else return content; + string bodyHtml = bodyMatch.Success ? bodyMatch.Groups[1].Value : content; + + // Convert to plain text for cleaner error output. + bodyHtml = Regex.Replace(bodyHtml, @"]*>[\s\S]*?", string.Empty, RegexOptions.IgnoreCase); + bodyHtml = Regex.Replace(bodyHtml, @"]*>[\s\S]*?", string.Empty, RegexOptions.IgnoreCase); + string text = Regex.Replace(bodyHtml, @"<[^>]+>", " "); + text = WebUtility.HtmlDecode(text); + return Regex.Replace(text, @"\s+", " ").Trim(); } } } \ No newline at end of file