Skip to content

Commit

Permalink
Merge pull request #28 from taori/bugfix-crash-onempty-contentType
Browse files Browse the repository at this point in the history
NRE fix for Unauthorized requests
[release]
  • Loading branch information
madskristensen authored May 10, 2022
2 parents 554b0e5 + ddb871f commit 5b3c3c9
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/RestClientVS/Margin/ResponseControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5b3c3c9

Please sign in to comment.