WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/03/2016 04:04:41 AM (4 years ago)
Author:
joehoyle
Message:

REST API: Return error when JSON decoding fails.

If you send a request to the REST API with invalid JSON in body than it will now return a error. This assists developers if they accidentally send invalid JSON and wonder why their data appears to be ignored.

Props rmccue.
Fixes #38547.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-request.php

    r39087 r39109  
    398398        $this->assertWPError( $valid );
    399399        $this->assertEquals( 'rest_invalid_param', $valid->get_error_code() );
     400    }
     401
     402    public function test_has_valid_params_json_error() {
     403        if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
     404            return $this->markTestSkipped( 'JSON validation is only available for PHP 5.3+' );
     405        }
     406
     407        $this->request->set_header( 'Content-Type', 'application/json' );
     408        $this->request->set_body( '{"invalid": JSON}' );
     409
     410        $valid = $this->request->has_valid_params();
     411        $this->assertWPError( $valid );
     412        $this->assertEquals( 'rest_invalid_json', $valid->get_error_code() );
     413        $data = $valid->get_error_data();
     414        $this->assertEquals( JSON_ERROR_SYNTAX, $data['json_error_code'] );
    400415    }
    401416
Note: See TracChangeset for help on using the changeset viewer.