Search code examples
c#flurl

Posting Multiple Headers with Flurl


Hi I'm using Flurl and I need to set multiple headers for the post and the documentation on the site states to do await url.WithHeaders(new { h1 = "foo", h2 = "bar" }).GetJsonAsync();

I'm not sure what this means, what is H1, H2 ?

I'm trying to set Headers "API-VERSION:21" and "Authorization: askjdalksdjlaksjdlaksjd";


Solution

  • Use documentation (a very beautiful one): https://flurl.dev/docs/fluent-http/

    // one: 
    await url.WithHeader("someheader", "foo").GetJsonAsync();
    
    // multiple: 
    await url.WithHeaders(new { h1 = "foo", h2 = "bar" }).GetJsonAsync();
    

    h1 and h2 are names of headers, and "foo" and "bar" are values. As you can see you may also use call .WithHeader("headerName", "headerValue") in your case:

    .WithHeader("API-VERSION", "21")
    .WithHeader("Authorization", "askjdalksdjlaksjdlaksjd")
    

    In other words chain calls to send multiple headers.