Search code examples
c#httpwebrequestwebrequest

Setting HttpWebRequest Header


I would like to send a POST request to a specified Endpoint. And for the authorisation I'have to set a "x-api-key: key-value pair to the header of my request.

This is what I'm using:

public string postXMLData(string destinationUrl, string requestXml)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destinationUrl);

            byte[] bytes;
            bytes = System.Text.Encoding.ASCII.GetBytes(requestXml);
            request.ContentType = "text/xml; encoding='utf-8'";
            request.ContentLength = bytes.Length;
            request.Method = "POST";

...

Solution

  • You simply need to add:

    request.Headers.Add("x-api-key", "the secret key");
    

    where "the secret key" is your API key.