WordPress.org

Make WordPress Core

Opened 7 months ago

Closed 7 months ago

Last modified 7 months ago

#38873 closed defect (bug) (fixed)

REST API doesn't match with trailing slash when not using rewrites

Reported by: joehoyle Owned by: rmccue
Milestone: 4.7 Priority: normal
Severity: normal Version:
Component: REST API Keywords: has-patch
Focuses: Cc:

Description (last modified by jnylen0)

Current, a request to the API with rewrite rules enabled doesn't differentiate URLs with trailing slashes just as http://localhost:8080/wp-json/wp/v2/ vs http://localhost:8080/wp-json/wp/v2, this seems to be a property to WP_Rewrite / class WP to split off the trailing slash always.

However, when making requests do http://localhost:8080/?rest_route=/wp/v2/ the trailing slash is not stripped, as it doesn't go through the same transformation in class WP. I think it's safe to always untrailingslash the requested URL in the REST API, for matching purposes.

Attachments (1)

38873.diff (471 bytes) - added by joehoyle 7 months ago.

Download all attachments as: .zip

Change History (6)

@joehoyle
7 months ago

#1 @jnylen0
7 months ago

  • Description modified (diff)

Similarly, the official API console is broken on my site due to a duplicated slash in between the REST prefix and the namespace (wp-json//wp/v2/posts):

https://nylen.io/rest-api-console-double-slash.png

Other cases:

  1. //wp-json/wp/v2/posts: works.
  2. //wp-json/wp//v2/posts: does not work. (Debatable whether it should, as wp/v2 is a valid namespace but wp//v2 is not.)
  3. //wp-json/wp/v2//posts: does not work.

#2 @joehoyle
7 months ago

wp-json/wp/v2/posts:

I think this one may depend on the server setup, could be wrong though.

#3 @rmccue
7 months ago

  • Keywords has-patch added
  • Owner set to rmccue
  • Status changed from new to reviewing

I'm happy with multiple slashes not working, tbh, as they are different URLs. Stripping the trailing slash seems like a no-brainer though.

#4 @rmccue
7 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 39329:

REST API: Trim trailing slashes from routes.

WordPress' rewrites do this usually, but the behaviour was inconsistent when using non-pretty permalinks.

Props joehoyle.
Fixes #38873.

#5 @jnylen0
7 months ago

This hack gets the API console working again on my test sites. I don't think there would be any harm in removing leading slashes from the route as well - it's apparently an easy place to make a mistake.

$ cat wp-content/plugins/fix-rest-api-console.php 
<?php

/**
 * Plugin Name: Fix the REST API console (`wp-json//` → `wp-json/`)
 * Author: James Nylen
 * Plugin URI: https://core.trac.wordpress.org/ticket/38873
 */

add_filter( 'rest_pre_dispatch', function( $result, $server, $request ) {
        $route = $request->get_route();
        $route_fixed = preg_replace( '@^/+@', '/', $route );
        if ( $route_fixed !== $route ) {
                $request->set_route( $route_fixed );
        }
        return $result;
}, 10, 3 );
Note: See TracTickets for help on using tickets.