[wp-trac] [WordPress Trac] #49680: Consider also allowing POST requests for block ServerSideRender endpoint

WordPress Trac noreply at wordpress.org
Sun Mar 22 23:40:34 UTC 2020


#49680: Consider also allowing POST requests for block ServerSideRender endpoint
--------------------------+-----------------------------
 Reporter:  ryankienstra  |      Owner:  (none)
     Type:  enhancement   |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:  rest-api      |
--------------------------+-----------------------------
 The
 [https://github.com/WordPress/gutenberg/tree/b16542e1ac508a6d31f1c87680aa73b1af5db3bf/packages
 /server-side-render ServerSideRender] component uses GET requests to send
 the attributes to its endpoint.

 But sometimes the attributes are too big to send in the URL of the GET
 request, and cause an error:

 https://github.com/WordPress/gutenberg/issues/19935
 https://github.com/WordPress/gutenberg/issues/16396#issuecomment-508709339
 https://wordpress.org/support/topic/error-loading-block-the-response-is-
 not-a-valid-json-response-2/

 There have been suggestions in those issues to allow using a POST request
 for `<ServerSideRender>`. This would allow sending a much bigger
 attributes object to the endpoint, and prevent this error.

 As blocks become more ubiquitous, like with Full Site Editing, I think
 this would help.

 The patch for this would probably be like:

 {{{
 diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-
 renderer-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-
 rest-block-renderer-controller.php
 index 94d349a1ba..bc1dbd3e83 100644
 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-
 controller.php
 +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-
 controller.php
 @@ -52,7 +52,7 @@ class WP_REST_Block_Renderer_Controller extends
 WP_REST_Controller {
                                                 ),
                                         ),
                                         array(
 -                                               'methods'             =>
 WP_REST_Server::READABLE,
 +                                               'methods'             =>
 array( WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ),
                                                 'callback'            =>
 array( $this, 'get_item' ),
                                                 'permission_callback' =>
 array( $this, 'get_item_permissions_check' ),
                                                 'args'                =>
 array(
 @@ -148,7 +148,8 @@ class WP_REST_Block_Renderer_Controller extends
 WP_REST_Controller {
                         );
                 }

 -               $attributes = $request->get_param( 'attributes' );
 +               // In a POST request, the attributes appear as JSON in the
 request body.
 +               $attributes = WP_REST_Server::CREATABLE ===
 $request->get_method() ? json_decode( $request->get_body(), true ) :
 $request->get_param( 'attributes' );

                 // Create an array representation simulating the output of
 parse_blocks.
 }}}

 Thanks!

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/49680>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list