如何通过发布请求将数据传递到其模式是WordPress REST API中的对象数组的字段

问题描述 投票:0回答:1

所以只从WordPress REST API开始

我正在创建我自己的REST API控制器(意味着有我自己的类扩展了\ WP_REST_Controller类)

我正在公开自定义帖子类型的API路由

代码很长,所以我不会在这里粘贴所有代码,只是重要的代码

到目前为止,一切正常,我能够从路由读取数据并通过路由创建数据

我遇到的一个问题是,我无法通过POST请求发送模式为对象数组的字段的数据

因此有此架构

    $schema = array(
        '$schema'    => 'http://json-schema.org/draft-04/schema#',
        'title'      => $this->post_type,
        'type'       => 'object',
        'properties' => array(
            'id'          => array(
                'description' => __( 'Unique identifier for the survey cpt object.', 'wp-plugin-boilerplate' ),
                'type'        => 'integer',
                'context'     => array( 'view', 'edit', 'embed' ),
                'readonly'    => true,
            ),
            'title'             => array(
                'description' => __( 'The title for the survey cpt object.', 'wp-plugin-boilerplate' ),
                'type'        => 'string',
                'context'     => array( 'view', 'edit', 'embed' ),
            ),
            'description' => array(
                'description' => __( 'The description for the survey cpt object', 'wp-plugin-boilerplate' ),
                'type'        => 'string',
                'context'     => array( 'view', 'edit', 'embed' )
            ),
            'status'      => array(
                'description' => __( 'A named status for the survey cpt object.', 'wp-plugin-boilerplate' ),
                'type'        => 'string',
                'enum'        => array_keys( get_post_stati( array( 'internal' => false ) ) ),
                'context'     => array( 'view', 'edit' ),
            ),
            'type'        => array(
                'description' => __( 'Type of Post for the survey cpt object.', 'wp-plugin-boilerplate' ),
                'type'        => 'string',
                'context'     => array( 'view', 'edit', 'embed' ),
                'readonly'    => true,
            ),
            'fields'      => array(
                'description' => __( 'Set of fields for the survey cpt object.', 'wp-plugin-boilerplate' ),
                'type'        => 'array',
                'context'     => array( 'view', 'edit', 'embed' ),
                'items'       => array(
                    'type'       => 'object',
                    'properties' => array(
                        'field_type'  => array( 'type' => 'string' ),
                        'description' => array( 'type' => 'string' ),
                        'order'       => array( 'type' => 'integer' )
                    )
                )
            )
        )
    );

所以fields字段是对象数组

为了避免麻烦,在此同时忽略身份验证,我只是允许通过REST API atm公开创建cpt项。

当我通过postman这样发送POST请求时,>

enter image description here

注意:

如果我删除/不通过字段字段,则上述操作成功。

所以我定义架构的方式有问题吗?或发送数据时出现问题?

提前感谢。

编辑:

也尝试过此

enter image description here

编辑:

确认这仅对于PostMan

是一个问题,通过AXIOS发送POST请求可以正常工作]

所以从WordPress REST API开始,我正在创建自己的REST API控制器(意味着我有自己的类扩展了\ WP_REST_Controller类),我正在公开自定义帖子的API路由...

wordpress postman wordpress-rest-api
1个回答
0
投票
代替

form-data选项

© www.soinside.com 2019 - 2024. All rights reserved.