MFedatto
MFedatto

Reputation: 134

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.

Upvotes: 2

Views: 2535

Answers (2)

intotecho
intotecho

Reputation: 5684

To read the headers from an Flurl response, I used this

 IFlurlResponse response = await request.WithHeaders(
    new { Accept = "application/xml" }).GetAsync();
 IReadOnlyNameValueList<string> headers = response.Headers; 
 string contentType = headers.FirstOrDefault(h => h.Name == "Content-Type").Value;
 return await response.GetStreamAsync();

This gives contentType="image/png".

Upvotes: 3

Todd Menier
Todd Menier

Reputation: 39319

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

Upvotes: 1

Related Questions