1

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);
Dominik
  • 116
  • 10
  • Maybe, this article will help you "Create a DelegatingHandler for X-HTTP-Method-Override" https://www.infoworld.com/article/3249687/how-to-implement-a-delegatinghandler-for-x-http-method-override-in-web-api.html – Radost Jun 22 '22 at 07:14

1 Answers1

0

This should work like normal:

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

I'm assuming you want PATCH as verb.

nvoigt
  • 75,013
  • 26
  • 93
  • 142