Skip to content

Commit

Permalink
Add authorization header to webhook requests separately. (#1411)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusmester authored Apr 14, 2021
1 parent 3d067a6 commit 1c216a7
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/WebHooks.Common/HttpWebHookClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public async Task SendAsync(string url, string httpMethod = null, object postDat

try
{
AddWellKnownHeaders(client, headers);

HttpResponseMessage response;
switch (httpMethod.ToUpper())
{
Expand All @@ -60,22 +62,40 @@ public async Task SendAsync(string url, string httpMethod = null, object postDat
break;
}

_logger.LogTrace($"WebHook service request completed with {response.StatusCode}. Url: {url} Http method: {httpMethod}");
var msg = $"WebHook service request completed with {response.StatusCode}. Url: {url} Http method: {httpMethod}";

if (response.IsSuccessStatusCode)
_logger.LogTrace(msg);
else
_logger.LogWarning(msg);
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"Error during webhook service call. Url: {url} Http method: {httpMethod}");
}
}

private static void AddWellKnownHeaders(HttpClient client, IDictionary<string, string> headers)
{
if (headers == null)
return;

if (headers.TryGetValue("Authorization", out var authValue))
{
client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authValue);
}
}

private static readonly string[] HeadersToSkip = new[] {"Authorization"};

private static StringContent GetStringContent(object postData, IDictionary<string, string> headers)
{
var postText = postData == null ? string.Empty : JsonConvert.SerializeObject(postData);
var content = new StringContent(postText, Encoding.UTF8, "application/json");

if (headers?.Any() ?? false)
{
foreach (var header in headers)
foreach (var header in headers.Where(h => !HeadersToSkip.Contains(h.Key)))
{
content.Headers.Add(header.Key, header.Value);
}
Expand Down

0 comments on commit 1c216a7

Please sign in to comment.