Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting response header

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 .

like image 528
Raghaven 3534 Avatar asked Apr 19 '17 07:04

Raghaven 3534


People also ask

How do I get response header responses?

View headers with browser development tools Select the Network tab. You may need to refresh to view all of the requests made for the page. Select a request to view the corresponding response headers.

How do I get the response header cookie?

Just set the Set-Cookie header in the response from the server side code. The browser should save it automatically. As a developer, you may be able to inspect the value of the cookies using "Developer Tools". And the same cookie will be sent in subsequent requests to the same domain, until the cookie expires.

Where do response headers come from?

Most actionable response headers are generated by the Web server itself. These include instructions for the client to cache the content (or not), content language, and the HTTTP request status code among others.


1 Answers

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.

like image 125
Ilya Chumakov Avatar answered Sep 23 '22 20:09

Ilya Chumakov