diff --git a/APSIM.POStats.Shared/WebUtilities.cs b/APSIM.POStats.Shared/WebUtilities.cs index 042ed9e..8457794 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,30 @@ 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; + + // Avoid regex work for non-HTML responses (e.g. JSON). + if (content.IndexOf("]*>([\s\S]*?)", RegexOptions.IgnoreCase); + 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