Created
December 19, 2020 22:50
-
-
Save lucas-zimerman/59fa93f65214d6e977098ff375861675 to your computer and use it in GitHub Desktop.
HttpResponseMessage to Sentry Protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//This function creates a Sentry Request based on HttpResponseMessage | |
public static async Task<Request> ToSentryRequestAsync(this HttpResponseMessage responseMessage) | |
{ | |
var request = new Request(); | |
try | |
{ | |
var response = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false); | |
request.Other.Add("server-response", response); | |
} | |
catch | |
{ | |
// HttpContent was disposed... | |
} | |
//Filter the Uri | |
if (responseMessage.RequestMessage?.RequestUri != null) | |
{ | |
request.Url = responseMessage.RequestMessage.RequestUri.GetUriPathWithoutQueryString(); | |
request.QueryString = responseMessage.RequestMessage.RequestUri.Query; | |
} | |
request.Method = responseMessage.RequestMessage?.Method.Method; | |
if (responseMessage.RequestMessage?.Content != null) | |
{ | |
try | |
{ | |
request.Data = await responseMessage.RequestMessage.Content.ReadAsByteArrayAsync().ConfigureAwait(false); | |
} | |
catch | |
{ | |
// Content was disposed... | |
} | |
} | |
foreach (var header in responseMessage.Content.Headers.ToDictionary(k => k.Key, v => string.Join(";", v.Value))) | |
{ | |
request.Headers.Add(header.Key, header.Value); | |
} | |
return request; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment