Search code examples
httpcontent

c# httpcontent - add Header If-Match errors


adding new header "If-Match"

 using (HttpContent content = new StringContent(serializedObject))
        {
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
            content.Headers.Remove("If-Match");
            content.Headers.Add("If-Match", "XXXXXXXXXX");
        }

throws :

Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.

i can add any other headers fine


Solution

  • edited:

    using(var request = new HttpRequestMessage(HttpMethod.Put, new Uri(url))) {
        request.Headers.Remove("If-Match");
        request.Headers.TryAddWithoutValidation("If-Match", "XXXXXXXXXX");
        using (HttpContent content = new StringContent(serializedObject))
        {
            content.Headers.Remove("Content-Type");
            content.Headers.Add("Content-Type", "application/json");
        }
        // ...
    }