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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?