From 9f502abafd0011d224c7da1e0eed08c5fbe4e58f Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Mon, 12 Oct 2020 20:09:14 +0000 Subject: [PATCH] REST API: Add HTTP/1.0 emulation to wp.apiRequest(). This allows for making REST API calls with the PUT and DELETE HTTP methods that may be blocked or unsupported by some server configurations. Props yakimun. Fixes #43605. git-svn-id: https://develop.svn.wordpress.org/trunk@49133 602fd350-edb4-49c9-b593-d223f7449a82 --- src/js/_enqueues/wp/api-request.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/js/_enqueues/wp/api-request.js b/src/js/_enqueues/wp/api-request.js index 51df7f6f64..885e5b47f8 100644 --- a/src/js/_enqueues/wp/api-request.js +++ b/src/js/_enqueues/wp/api-request.js @@ -9,6 +9,7 @@ * - Allows specifying only an endpoint namespace/path instead of a full URL. * * @since 4.9.0 + * @since 5.6.0 Added overriding of the "PUT" and "DELETE" methods with "POST". * @output wp-includes/js/api-request.js */ @@ -23,6 +24,7 @@ apiRequest.buildAjaxOptions = function( options ) { var url = options.url; var path = options.path; + var method = options.method; var namespaceTrimmed, endpointTrimmed, apiRoot; var headers, addNonceHeader, headerName; @@ -76,10 +78,23 @@ }, headers ); } + if ( typeof method === 'string' ) { + method = method.toUpperCase(); + + if ( 'PUT' === method || 'DELETE' === method ) { + headers = $.extend( { + 'X-HTTP-Method-Override': method + }, headers ); + + method = 'POST'; + } + } + // Do not mutate the original options object. options = $.extend( {}, options, { headers: headers, - url: url + url: url, + method: method } ); delete options.path;