duanci1858 2016-12-12 06:31
浏览 37

wordpress rest-api问题与空请求正文通过PHP 7

I am using wordpress 4.7 on php 7, also getting problems with the empty request body.

My Controller :

add_action('rest_api_init', function()
{
     register_rest_route('v0', '/accounts/(?P<slug>[a-z0-9_\-]+)/accounts', array(
        'methods' => 'GET',
        'callback' => function($request)
        {
            try {
               return 'hello';
            }
            catch (Exception $e) {
                $error = json_decode($e->getMessage(), true);
                return new WP_Error($error['status_code'], $error['message'], "");
            }
        }
    ));
});

Response Error :

{"code":"rest_invalid_json","message":"Invalid JSON body passed.","data":{"status":400,"json_error_code":4,"json_error_message":"Syntax error"}}

I did not found any solution rather than changing in WP core : wp-includes/rest-api/class-wp-rest-request.php and chaning line 672 for conditional check for empty body or not.

$params = json_decode( $this->get_body(), true );
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!