Getting response header in Flurl/C#

Getting response header in Flurl/C#



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 .





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


GetJsonAsync




1 Answer
1



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:


GetJsonAsync<T>


Task<T>


GetAsync


HttpResponseMessage response = await url.GetAsync();

HttpResponseHeaders headers = response.Headers;

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



ReadFromJsonAsync is an extention method:


ReadFromJsonAsync


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.


HttpClient





or, you could just read the documentation of your chosen 3rd party library ;)
– Jamiec
Apr 19 '17 at 7: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 7:46


HttpResponseMessage





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 7:49





@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






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế