From ddb871fc6d8627573ad66c3c65f39ef02c720325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=BCller?= Date: Mon, 9 May 2022 14:54:45 +0200 Subject: [PATCH] NRE fix --- .../Margin/ResponseControl.xaml.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/RestClientVS/Margin/ResponseControl.xaml.cs b/src/RestClientVS/Margin/ResponseControl.xaml.cs index 0cea067..f4c7141 100644 --- a/src/RestClientVS/Margin/ResponseControl.xaml.cs +++ b/src/RestClientVS/Margin/ResponseControl.xaml.cs @@ -20,13 +20,30 @@ public async Task SetResponseTextAsync(RequestResult result) var text = new StringBuilder(); text.AppendLine(result.Response.Headers.ToString()); text.AppendLine(result.Response.Content.Headers.ToString()); - var mediaType = result.Response.Content.Headers.ContentType.MediaType; + var mediaType = result.Response.Content.Headers?.ContentType?.MediaType; + if (mediaType == null) + { + var rawString = await result.Response.Content.ReadAsStringAsync(); + text.AppendLine("------------------------------------------------"); + text.AppendLine($"StatusCode: {result.Response.StatusCode} ({(int)result.Response.StatusCode})"); + if (!string.IsNullOrEmpty(rawString)) + { + text.AppendLine("Content:"); + text.AppendLine(rawString); + } + + Control.Text = text.ToString(); + return; + } if (mediaType.IndexOf("json", StringComparison.OrdinalIgnoreCase) > -1) { var jsonString = await result.Response.Content.ReadAsStringAsync(); try { var token = JToken.Parse(jsonString); + text.AppendLine("------------------------------------------------"); + text.AppendLine($"StatusCode: {result.Response.StatusCode} ({(int)result.Response.StatusCode})"); + text.AppendLine("Content:"); text.AppendLine(token.ToString()); } catch (Exception ex) @@ -43,6 +60,9 @@ public async Task SetResponseTextAsync(RequestResult result) try { var xmlElement = XElement.Parse(xmlString); + text.AppendLine("------------------------------------------------"); + text.AppendLine($"StatusCode: {result.Response.StatusCode} ({(int)result.Response.StatusCode})"); + text.AppendLine("Content:"); text.AppendLine(xmlElement.ToString()); } catch (Exception ex)