【问题标题】:Custom wordpress REST API to accept JSON formatted input自定义 wordpress REST API 以接受 JSON 格式的输入
【发布时间】:2017-05-24 16:22:25
【问题描述】:

我正在使用 WP REST v2 编写 wordpress REST API。有没有一种方法可以在我们在 register_rest_route 函数中定义的回调函数中处理传入的 JSON 参数(而不是查询参数)?

例如:

function wpplugin_register_routes() {
    register_rest_route( 'testapi/v1', 'users', array(
        'methods'  => 'POST',
        'callback' => 'wpplugin_process_json_params',
    ) );
}

function wpplugin_process_json_params( WP_REST_Request $request ) {

    // Process the $request which should be a JSON string
}

【问题讨论】:

    标签: wordpress wordpress-rest-api


    【解决方案1】:

    找到了解决方案。 WP_REST_Request 对象包含 JSON 参数,因此可以使用与 GET / POST 参数相同的 $request['parameter_name'] 检索它。

    【讨论】:

      猜你喜欢
      • 2021-10-31
      • 2014-05-06
      • 2017-09-23
      • 1970-01-01
      • 2023-03-10
      • 2020-09-22
      • 2017-10-29
      • 1970-01-01
      • 2011-08-22
      相关资源
      最近更新 更多