7

Used the Flurl to Get response from API.

var response = await url.WithClient(fc)
            .WithHeader("Authorization", requestDto.ApiKey)
            .GetJsonAsync<T>();
dynamic httpResponse = response.Result;

But I cant able to access httpResponse.Headers

How to access response headers while using GetJsonAsync .

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Raghaven 3534
  • 286
  • 1
  • 5
  • 10
  • Do you care about the headers in all scenarios or just error responses, such as 403s? If the latter, Flurl has a handy try/catch pattern where you can get at the response headers without abandoning `GetJsonAsync`. – Todd Menier Jun 13 '17 at 16:44

2 Answers2

8

You can't get a header from GetJsonAsync<T> because it returns Task<T> instead of raw response. You can call GetAsync and deserialize your payload at next step:

HttpResponseMessage response = await url.GetAsync();

HttpResponseHeaders headers = response.Headers;

FooPayload payload = await response.ReadFromJsonAsync<FooPayload>();

ReadFromJsonAsync is an extention method:

public static async Task<TBody> ReadFromJsonAsync<TBody>(this HttpResponseMessage response)
{
    if (response.Content == null) return default(TBody);

    string content = await response.Content.ReadAsStringAsync();

    return JsonConvert.DeserializeObject<TBody>(content);
}

P.S. This is why I prefer and recommend to use raw HttpClient instead of any third-party high-level client like RestSharp or Flurl.

Luke Girvin
  • 13,221
  • 9
  • 64
  • 84
Ilya Chumakov
  • 23,161
  • 9
  • 86
  • 114
  • or, you could just read the documentation of your chosen 3rd party library ;) – Jamiec Apr 19 '17 at 07:43
  • @Jamiec do you know a good 3rd party http client library? RestSharp is abandoned and Flurl forces to use `HttpResponseMessage` for low-level operations. I really don't see any benefits of Flurl instead of HttpClient. – Ilya Chumakov Apr 19 '17 at 07:46
  • I just wrote my own for my use. Its modeled a little on flurl (has a fluent interface, built mainly as extensions) because, like you, I generally dont like 3rd party libs for this simple task - but I do the same operation in very slightly different ways so it makes sense to wrap it up. – Jamiec Apr 19 '17 at 07:49
  • 4
    @IlyaChumakov What you're describing as a downside of Flurl ("forces you to use HttpResponseMessage for low-level operations") was actually intended to be an advantage. Flurl's main goal is modest: save keystrokes in the most common 95% of scenarios. And for the other 5%, make the underlying HttpClient APIs easily accessible (as you've demonstrated here) so that you're never stuck. (And yes, I completely made up those numbers. :) – Todd Menier Jun 13 '17 at 16:40
4

You could also await for the HttpResponseMessage, pick off the .Headers object, then send the completed task to ReceiveJson<T> for deserialization. Here's how to do it without an extension method:

var task = url.GetAsync();

HttpResponseMessage response = await task;

HttpResponseHeaders headers = response.Headers;

//Get what I need now from headers, .ReceiveJson<T>() will dispose
//response object above.

T obj = await task.ReceiveJson<T>();
Brian Chavez
  • 8,048
  • 5
  • 54
  • 47