Custom Rest API namespace and endpoints are responding with 404 & 503 errors
I am new to WordPress plugin development. I was developing a plugin for WordPress that would be used to create mobile apps for WordPress Bloggers.
The whole source code of the plugin is at:- https://github.com/delhideveloper/Delhi-Developer-PLugin-For-Blogger-Mobile-Apps
I have implemented the JWT Authentication in the plugin using firebase JWT. I know there was already a plugin for implementing JWT authentication but it was not improved since 2 years and I was facing some problems with it.
I have used only POST requests for every single API request just for simplicity. I hope it is not a crime to do so.
I have completely implemented the mobile app and the plugin. But there is a serious problem that I am now facing:-
The Rest API is the problem.
- Nearly 1/3 rd of the times it works perfectly and gives me perfect JSON response.
- Nearly 1/3 rd of the times it gives a 404 Error of this kind:-
{ "code": "rest_no_route", "message": "No route was found matching the URL and request method", "data": { "status": 404 } }
- Nerly 1/3 rd of the time it gives me this 503 Error:-
503 Service UnavailableService Unavailable
The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.
Additionally, a 503 Service Unavailable error was encountered while trying to use an ErrorDocument to handle the request.
I am struggling since a month and have not been able to figure out if it is a problem with server configuration, wordpress cofiguration, .htaccess, or my bad coding.
**Another important point to notice: ** The inbuilt WordPress endpoints are working flawlessly.
Refreshing the permalinks is not helping.
Changing post requests to get requests did not help.
I have tried my plugin on multiple websites. I have the same problem everywhere.
You can just try both the endpoints:-
Wordpress Inbuilt:
(GET) https ://delhideveloper.com/wordpress_blogger_app_demo/wp-json/wp/v2/posts
(GET) https ://delhideveloper.com/wordpress_blogger_app_demo/wp-json/wp/v2/post
My endpoints:-
https ://delhideveloper.com/wordpress_blogger_app_demo/wp-json/delhideveloper/v1/posts
https ://delhideveloper.com/wordpress_blogger_app_demo/wp-json/delhideveloper/v1/videos
https ://delhideveloper.com/wordpress_blogger_app_demo/wp-json/delhideveloper/v1/top_level_categories
Please add a comment if I should provide some more information about the problem.
My rest api code:-
/* Creating a new API End Point : User Registration */ add_action( 'rest_api_init', function () { register_rest_route( 'delhideveloper/v1', '/posts', array( 'methods' => 'POST', 'callback' => 'dd_retreive_posts_json', ) ); } ); function dd_retreive_posts_json( $request ) { $posts_per_page = 10; $current_page = 1; $current_page = $request->get_param("current_page"); $posts = get_posts( array( 'posts_per_page' => $posts_per_page, 'offset'=> ($current_page-1) * $posts_per_page , ) ); if( ! $posts ) { $response = new stdClass(); $response->code = 'Failure'; $response->message = 'No Posts Could Be Retreived!'; $response->posts = array(); return new WP_REST_Response( $response , 200 ); } $posts_array = array(); foreach( $posts as $post ) { $posts_object = new stdClass(); $posts_object->id = $post->ID; $posts_object->title = $post->post_title; $posts_object->thumbnail = get_the_post_thumbnail($post->ID); //$posts_object->url = 'https://prachyakarma.com/'. $post->post_name; //$posts_object->content = $post->post_content; //$posts_object->excerpt = mb_substr( $post->post_content , 0 , 100 , 'UTF-8' ); //$posts_object->date = date( 'M j, Y' , strtotime($post->post_date) ); array_push( $posts_array , $posts_object ); } $response = new stdClass(); $response->code = 'Success'; $response->message = 'Posts Retreived'; $response->posts = $posts_array; return new WP_REST_Response( $response , 200 ); }