Samw
Samw

Reputation: 128

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

Upvotes: 2

Views: 690

Answers (1)

user326608
user326608

Reputation: 2548

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");
    }
    // ...
}

Upvotes: 1

Related Questions