简体   繁体   中英

wordpress rest api custom endpoint - add header in request API

it's my first time to create rest api with custom endpoint in wordpress the api is working fine but the problem is i want to add key and value in the header when i send request to the api , to avoid using the api in postman without adding these key and value in the header here is the code that create the api :

add_action( 'rest_api_init', 'wl_posts');
function wl_posts(){
 register_rest_route('w1/v1','fav/',[
   'methods' => 'POST',
   'callback' => 'test',
   'header'  => 'username : test'
]);

any help ?

You can create a function from you callback that gathers the param you need, and do what you need after:

<?php

function callback_name( $data ) {
    
    $your_var = $data->get_param( 'param_name' );
    
    // Do other stuff that you need to do

}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM