• Resolved Julian Holzmayer

    (@julholdroid)


    Hello guys,

    I have a question according to the REST API for updating a cmb2 field value.

    This is the code for the registration of he field and the box:

    $cmb = new_cmb2_box(
    			array(
    				'id'           => 'hd-object-viewer-mb',
    				'title'        => __( '3D View Scene', 'hd-3d-viewer' ),
    				'object_types' => array( 'hd-object-viewer' ),
    				'context'      => 'normal',
    				'priority'     => 'high',
    				'show_names'   => true,
    				'show_in_rest' => true
    			)
    		);
    
    		$cmb->add_field(
    			array(
    				'name' => 'Test Text Area',
    				'desc' => 'This is the json data',
    				'id'   => 'hd-object-viewer-mb-scene-json',
    				'type' => 'textarea',
    				'sanitization_cb' => false,
    				'escape_cb'			=> false,
    			)
    		);

    I registerd my CPT like this:

    register_post_type(
    			'hd-object-viewer',
    			array(
    				'label'               => __( '3D View', 'hd-3d-viewer' ),
    				'description'         => 'Recipe custom post type.',
    				'public'              => true,
    				'exclude_from_search' => true,
    				'show_in_rest' => true,
    				'supports'            => array( 'title' ),
    				'menu_icon'           => 'dashicons-admin-site',
    			)
    		);

    I now want to update the field via the REST API. When i make a GET Request to /wp-json/cmb2/v1/boxes/hd-object-viewer-mb/fields/hd-object-viewer-mb-scene-json i get the field back (so the field is registered in the API)

    But when i do a POST request to the same URL i get an error:

    {
        "code": "cmb2_rest_no_field_by_id_error",
        "message": "No field found by that id.",
        "data": {
            "status": 403
        }
    }

    My POST body is:

    {
        "object_id": 49,
        "value": "other value test",
        "object_type": "post"
    }

    I also tried it with the “hd-object-viewer” (CPT) object_type which results in the same error.

    What is the object_id anyways. I considered it as the post id, maybe this is not correct.

    For Authentication i used a Application password with my admin User, so i am pretty sure i am authenticated at it would say otherwise. Maybe i don’t have the permission to change the metafield, which would be weird as this is the application password for the admin user.

    Any help would be appreciated.

    Thanks in Advance.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Justin Sternberg

    (@jtsternberg)

    See the REST API docs. It covers these routes and what the object_type is. If you still run into issues, let us know.
    https://github.com/CMB2/CMB2/wiki/REST-API#routes

    Plugin Author Justin Sternberg

    (@jtsternberg)

    Sorry, one last look at your payload and it looks correct except that you’re passing in the body instead of query args. Try following the recommendation in the docs with the query args.

    Thread Starter Julian Holzmayer

    (@julholdroid)

    Thanks you for your reply.

    Unfortunately i still get the same error, making a POST Request without any body to this route:
    /wp-json/cmb2/v1/boxes/hd-object-viewer-mb/fields/hd-object-viewer-mb-scene-json?object_id=49&value=other&object_type=post

    I use Postman to make the request and the id of the post where i want to change the metafield is 49.

    For Authtentication i created a application password for the admin user and passed it with Basic Auth like mentioned here in the request

    Ah and by the way i use:

    • WP: 6.3.1
    • CMB2 installed via composer inside my own plugin: “cmb2/cmb2”: “^2.10”
    Plugin Author Justin Sternberg

    (@jtsternberg)

    When testing this in my API app, Insomnia, I was seeing this same behavior (the POST request not updating the field). It turned out it was because I was using the wrong url (in my case http instead of https and the server was doing a redirect and changing to a GET).
    See screenshot here: https://a.supportally.com/i/hBDQ9a

    Check to see if you’re experiencing the same.

    You could also add some logging to your server to see what the request method is when it lands there. E.g.
    error_log( '$_SERVER[REQUEST_METHOD]: ' . print_r( $_SERVER['REQUEST_METHOD'], true ) );

    Thread Starter Julian Holzmayer

    (@julholdroid)

    Hello Justin, this did’t solve my problem (there was no redirect at my request happening. So the method allways was POST).

    As my plugin’s use case changed I don’t need the REST API anymore.

    But thank you anyway for looking into the problem.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘REST API Update field’ is closed to new replies.