skip to Main Content

i am working on a wordpress plugin that uses custom rest api routes. I have one function which uses GET and one which uses POST both callabacks have been registered. I can see the endpoints listed when i navigate to localhost/mysitename/wp-json/wpgpt/v1. When I navigate to testbubby or make a GET request in postman I receive: {
"code": "rest_invalid_handler",
"message": "The handler for the route is invalid.",
"data": {
"status": 500
}
} when i navigate to send-message I get 404 (But i think this is just because it only takes a post request) but when I make a POST request in postman i receive 500 error still.

This is my php code, register_wpgpt_api_routes() is called during construct.

function register_wpgpt_api_routes() {
    register_rest_route(
        'wpgpt/v1', 
        '/send-message', 
        array(
        'methods'  => 'POST',
        'callback' => 'send_message',
        'permission_callaback' => '__return_true'
    ));
    register_rest_route('wpgpt/v1', '/testbubby', array(
        'methods'  => 'GET',
        'callback' => 'test_route',
        'permission_callaback' => '__return_true'
    )
);
    
}

function test_route(){
    echo "hey there";
}


function send_message( string $message ): string {    
    $messages = [];

    if( isset( $this->system_message ) ) {
        $messages[] = [
            "role" => "system",
            "content" => $this->system_message,
        ];
    }
    //$messages = array_merge( $messages, $this->message_history );

    $messages[] = [
        "role" => "user",
        "content" => $message,
    ];

    return $this->make_api_request( $messages );
}

This is the JS function which is supposed to make the request but I get 404 when i click the button to execute it (But i can see the payload in my network tab):

 function wpgpt_send_message() {
    let message = $(".wpgpt-chat-input").val();
    wpgpt_add_message( message, "user" );
    $(".wpgpt-chat-input").val("");

    let payload = {
        "message": message,
        "message_history": wpgpt_message_history
    };

    $.ajax({
        url: '/wp-json/wpgpt/v1/send-message',
        method: 'POST',
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify( payload ),
        success: function( response ) {
            wpgpt_add_message( response.message, "assistant" );
            wpgpt_message_history.push( {
                "role": "user",
                "content": message,
            } );
            wpgpt_message_history.push( {
                "role": "assistant",
                "content": response.message.replace( "n", "<br>" ),
            } );
        },
        error: function() {
            wpgpt_add_message( "Sorry, there was an error", "assistant" );
        }
      });
} 

Appreciate any assistance, this is my first time trying to use the custom endpoints.

2

Answers


  1. Please read this entire page from the WordPress Docs: Adding Custom Endpoints

    You will see that the first argument passed into the callback is a WP_REST_Request object. The send_message() function expects a string as the first argument.

    You will also learn that the callback should return a value (e.g. PHP associative array) that can be serialized into a JSON string (which is done automatically by WordPress) to be returned to the client. The test_route() function is echo-ing content rather than return-ing a value.

    Another helpful page to read is Routes and Endpoints.

    Login or Signup to reply.
  2. In your function ‘send_message’ you need to return a rest response with your data and http status code

    return new WP_REST_Response( array( 'message' => 'Ok' ), 200 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search