Make WordPress Core

Changeset 39594


Ignore:
Timestamp:
12/13/2016 03:33:14 AM (8 years ago)
Author:
jnylen0
Message:

REST API: Do not error on empty JSON body

It's fairly common for clients to send Content-Type: application/json with an
empty body. While technically not valid JSON, we've historically supported
this behaviour, so it shouldn't cause an error.

Props JPry.
Fixes #39150.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-request.php

    r39563 r39594  
    670670        }
    671671
    672         $params = json_decode( $this->get_body(), true );
     672        $body = $this->get_body();
     673        if ( empty( $body ) ) {
     674            return true;
     675        }
     676
     677        $params = json_decode( $body, true );
    673678
    674679        /*
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r39563 r39594  
    1111 */
    1212class Tests_REST_Request extends WP_UnitTestCase {
     13    public $request;
     14
    1315    public function setUp() {
    1416        parent::setUp();
     
    449451        $data = $valid->get_error_data();
    450452        $this->assertEquals( JSON_ERROR_SYNTAX, $data['json_error_code'] );
     453    }
     454
     455
     456    public function test_has_valid_params_empty_json_no_error() {
     457        if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
     458            return $this->markTestSkipped( 'JSON validation is only available for PHP 5.3+' );
     459        }
     460
     461        $this->request->set_header( 'Content-Type', 'application/json' );
     462        $this->request->set_body( '' );
     463
     464        $valid = $this->request->has_valid_params();
     465        $this->assertNotWPError( $valid );
    451466    }
    452467
Note: See TracChangeset for help on using the changeset viewer.