X-HTTP-Method-Override For Rest Web API


Sometimes, some clients cannot send certain HTTP request types, such as PUT or DELETE. Also, most times, some production environments are not configured to accept some request types, such as PUT, PATCH e.t.c.

One of the tricks around this is to clients send  POST request and set the X-HTTP-Method-Override header to the desired method. Then create a custom message handler that overrides the original request method.

Create custom message handler that adds support for X-HTTP-Method-Override like so:

1:  using System;  
2:  using System.Linq;  
3:  using System.Net.Http;  
4:  using System.Threading;  
5:  using System.Threading.Tasks;  
6:  namespace MessagehandlerWithWebAPI.Filters  
7:  {  
8:    public class MethodOverrideHandler: DelegatingHandler  
9:    {  
10:      readonly string[] acceptedOverrideMethods = { "PUT", "DELETE", "PATCH" };  
11:      const string headerKey = "X-HTTP-Method-Override";  
12:      protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)  
13:      {  
14:        if(request.Method==HttpMethod.Post && request.Headers.Contains(headerKey))  
15:        {  
16:          var overrideMethod = request.Headers.GetValues(headerKey).FirstOrDefault();  
17:          if (overrideMethod != null && acceptedOverrideMethods.Contains(overrideMethod, StringComparer.InvariantCultureIgnoreCase))  
18:            request.Method = new HttpMethod(overrideMethod);  
19:        }  
20:        return base.SendAsync(request, cancellationToken);  
21:      }  
22:    }  
23:  }  

From above,
  • The declare an array of  methods clients can override.
  • In the SendAsync, the handler  checks if is a POST request, and also, if the header contains the X-HTTP-Method-Override.
  • The header value is validated and then modifies the request method.
  • The handler then calls base.SendAsync  to pass the request message to the next handler in the pipeline.
Register the custom handler in WebApiConfig class like so:

1:  using MessagehandlerWithWebAPI.Filters;  
2:  using System.Web.Http;  
3:  namespace MessagehandlerWithWebAPI  
4:  {  
5:    public static class WebApiConfig  
6:    {  
7:      public static void Register(HttpConfiguration config)  
8:      {  
9:        // Web API configuration and services  
10:        config.MessageHandlers.Add(new MethodOverrideHandler());  
11:        // Web API routes  
12:        config.MapHttpAttributeRoutes();  
13:        config.Routes.MapHttpRoute(  
14:          name: "DefaultApi",  
15:          routeTemplate: "api/{controller}/{id}",  
16:          defaults: new { id = RouteParameter.Optional }  
17:        );  
18:      }  
19:    }  
20:  }  

When the request reaches the HttpContollerDispatcher handler in the pipeline, it will route the request appropriately based on the updated request method.

I hope the article is helpful.

Your comments and feedback will be greatly appreciated.

Than you for your time.
X-HTTP-Method-Override For Rest Web API X-HTTP-Method-Override For Rest Web API Reviewed by Akintunde Toba on February 03, 2020 Rating: 5

No comments:

Home Ads

Powered by Blogger.