From 8fee9bfcf34ecc3b5c893b18c6c2ba45b80c26f6 Mon Sep 17 00:00:00 2001 From: Henrique Prange Date: Tue, 31 Jan 2017 13:47:29 -0500 Subject: [PATCH] Add support for X-HTTP-Method-Override header on REST requests Sometimes a client cannot consume REST services using methods like PUT, DELETE or PATCH, due to browser limitation or firewall rules. Some frameworks provide a workaround using the X-HTTP-Method-Override header to override the method of a request. No change to the route configuration is necessary. The client of the API just informs the override header, and the ERRest library maps routes as expected. Keeping a RESTful API on the server side is the main benefit of that approach. No need for alternate / unRESTfull routes creation. Scott Hanselman talked in more detail about that in [a blog post](http://www.hanselman.com/blog/HTTPPUTOrDELETENotAllowedUseXHTTPMethodOverrideForYourRESTServiceWithASPNETWebAPI.aspx). --- .../Sources/er/rest/routes/ERXRouteRequestHandler.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Frameworks/EOF/ERRest/Sources/er/rest/routes/ERXRouteRequestHandler.java b/Frameworks/EOF/ERRest/Sources/er/rest/routes/ERXRouteRequestHandler.java index 2cef00a1297..c96fef4670f 100644 --- a/Frameworks/EOF/ERRest/Sources/er/rest/routes/ERXRouteRequestHandler.java +++ b/Frameworks/EOF/ERRest/Sources/er/rest/routes/ERXRouteRequestHandler.java @@ -124,6 +124,11 @@ * @author mschrag */ public class ERXRouteRequestHandler extends WODirectActionRequestHandler { + /** + * Header used to override the method of a request. Useful when client code can use only GET and POST methods. + */ + private static final String X_HTTP_METHOD_OVERRIDE_HEADER_KEY = "x-http-method-override"; + /** * A NameFormat that behaves like Rails -- plural entities, plural routes, lowercase underscore names * (names_like_this). @@ -823,8 +828,9 @@ public NSArray getRequestHandlerPathForRequest(WORequest request) { try { String path = request._uriDecomposed().requestHandlerPath(); + String method = request.headerForKey(X_HTTP_METHOD_OVERRIDE_HEADER_KEY, request.method()); - ERXRoute matchingRoute = setupRequestWithRouteForMethodAndPath(request, request.method(), path); + ERXRoute matchingRoute = setupRequestWithRouteForMethodAndPath(request, method, path); if (matchingRoute != null) { @SuppressWarnings("unchecked") NSDictionary keys = (NSDictionary) request.userInfo().objectForKey(ERXRouteRequestHandler.KeysKey);