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
This commit is contained in:
Timothy Jacobs 2020-10-12 20:09:14 +00:00
parent c39f290124
commit 9f502abafd

View File

@ -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;