Search code examples
c#.netrestx-http-method-override

How do I pass X-HTTP-METHOD-Override Header to C#s HttpClient


I try to update a field using REST in C#. Using PatchAsync did not work so far, so I'm trying to use PUT instead, but now I need to pass the X-HTTP-METHOD-Override Header and I have no idea how to do this.

I've looked at the available headers for the DefaultRequestHeaders, but I could not find anything.

This is what I have got so far:

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri(BASEURL);

                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = CreateAuthHeader();
                var requestBody = new Dictionary<string, string>
                {
                    { "price", price.ToString() }
                };

                var jsonRequest = JsonConvert.SerializeObject(requestBody);
                var content = new StringContent(jsonRequest, Encoding.UTF8, "application/json-patch+json");

                var response = new HttpResponseMessage();

                response = await client.PutAsync($"{APIBASE}/data/ProductAttribute/{attributeId}", content).ConfigureAwait(false);

Solution

  • This should work like normal:

    client.DefaultRequestHeaders.Add("X-HTTP-METHOD-Override", "PATCH");
    

    I'm assuming you want PATCH as verb.