'Using PostAsync method from HttpClient Class with an API key in C#

I would like to write the equivalent of this curl script in C#. My curl script is the following:

curl -X POST \
https://example.com/login \
-H 'api-key: 11111111' \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-d '{
"username": "[email protected],
"password": "mypassword"
}'

The corresponding C# code I have wrote is the following:

async static void PostRequest()
{
    string url="example.com/login"
    var formData = new List<KeyValuePair<string, string>>();
    formData.Add(new KeyValuePair<string, string>("username", "[email protected]"));
    formData.Add(new KeyValuePair<string, string>("password", "mypassword"));
    HttpContent q = new FormUrlEncodedContent(formData);
    // where do I put my api key?
    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));//ACCEPT header
        client.BaseAddress = new Uri(url);
        using (HttpResponseMessage response = await client.PostAsync(url, q))
        {
            using (HttpContent content =response.Content)
            {
                string mycontent = await content.ReadAsStringAsync();              
            }        
        }
    }
}

My question is how do I include the Api Key to my request?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source