Search code examples
c#restloggingflurlself-signed-certificate

How to read my custom Flurl headers from HttpCall


How can I read the custom headers I've put on my FlurlRequest throug WithHeader(string name, object value) and the Authorization throug WithOAuthBearerToken(string token) from the HttpCall FlurlClient settings gives us on Func<HttpCall, Task> BeforeCallAsync()?

I've designed our solution with a FlurlClient to dinamically setup self-signed certificates and a custom JSON naming strategy, witch is mandatory to comunicate with this third party API.

I'm using Flurl.Http 2.4.2.0

_flurlClient = new FlurlClient()
    .Configure(settings =>
    {
        settings.HttpClientFactory = new X509HttpFactory(
            _certificateConfiguration.PersonalInfoExchangePath,
            _certificateConfiguration.ClientSecret);
        settings.JsonSerializer = _thirdPartyApiJsonSerializer;
        settings.BeforeCallAsync = async (httpCall) =>
        {
            thirdPartyRequestIdentity = await _appThirdPartyRequestRepository.Insert(
                new ThirdPartyRequest
                {
                    AppRequestId = await _contextAccessor.GetRequestId(),
                    RawContent = JsonConvert.SerializeObject(
                        new
                        {
                            Method = httpCall.Request
                                .Method
                                .ToString(),
                            Content = httpCall.Request.Content != null
                                ? await httpCall.Request
                                    .Content
                                    .ReadAsStringAsync()
                                : null,
                            Headers = httpCall.Request
                                .Content?.Headers
                                .Where(h => h.Value != null)
                                .Select(h => new
                                {
                                    h.Key,
                                    Value = h.Value.ToArray()
                                })
                                .ToArray(),
                            httpCall.Request.RequestUri.AbsoluteUri,
                            httpCall.RequestBody,
                            EnvironmentHostname = await _environmentContext.GetHostname(),
                            EnvironmentAddressList = (await _environmentContext.GetAddressList())
                                .Select(a => a.ToString())
                        },
                        _recoveryJsonSerializerSettings),
                    Timestamp = DateTime.Now
                });
        };
    });

I need to log all headers but with this code the only header I'm getting, when applicable, is the Content-Type header.


Solution

  • Get them from the IFlurlRequest rather than from the HttpRequestMessage. In other words, instead of this:

    httpCall.Request.Content?.Headers
    

    use this:

    httpCall.FlurlRequest.Headers