Home>

Making an API with the WP REST API plugin. Faced such a problem. Suppose I have a function

FUNCTION FOO () {
    ...
    Return New WP_REST_RESPONSE (NULL, 400);
}

If it is called as a normal function -everything is fine, IPP returns a blank answer (Content Length= 0) and the status 400. However, it is worth it to insert the same function in the class and the API for some reason inserts 1 symbol of dual quotes in response (Content Length= 1). What could be the reason and how can you get rid of it?

class code

class pdbcplugin_model {
        /*
            Constructor.
         * /
        Public Function __Construct () {
            $ this->
pdbc_table_management_install ();
            $ this->
pdbc_table_users_install (); //Functions for creating tables in database
        }
        Public Function PDBC_CREATE_USER (WP_REST_Request $ Request) {
             Global $ WPDB;
             $ managementLogin= $ Request->
get_param ('managementLogin');
             $ managementpassword= $ Request->
get_paramword ('managementpassword');
             $ table_name= $ wpdb->
Prefix. "Management";
             If (Empty ($ ManagementLogin) Or Empty ($ ManagementPassword)) {
                  Return New WP_REST_RESPONSE (NULL, 400);
             } ELSE {
                  $ rowcount= $ wpdb->
Get_Var ("Select Count (*) from $ Table_name Where ManagementLogin= '$ managementLogin' and ManagementPassword= '$ ManagementPassword'");
                  if ($ rowcount== 1) {
                      $ Name= ESC_SQL ($ Request->
get_param ('Name'));
                      $ email= ESC_SQL ($ Request->
get_param ('email'));
                      $ LastName= ESC_SQL ($ Request->
get_param ('LastName'));
                      $ password= esc_sql ($ Request->
get_param ('password'));
                      $ iSprivate= ESC_SQL ($ Request->
get_param ('isprivate'));
                      $ iSenable= ESC_SQL ($ Request->
get_param ('iSenable'));
                      If ((Empty ($ Name)) OR (Empty ($ LastName)) OR (Empty ($ Password)) OR (! ISSET ($ iSprivate)) OR (! isset ($ iSenable) )) {
                            Return New WP_REST_RESPONSE (NULL, 400);
                      } ELSE {
                            $ table_name= $ wpdb->
Prefix. "Projectusers";
                            $ rowcount= $ wpdb->
get_var ("Select Count (*) from $ table_name where email= '$ email");
                             if ($ rowcount== 1) {
                                  Return New WP_REST_RESPONSE (FALSE, 409);
                             } ELSE {
                                  $ wpdb->
Query ("Insert Into $ table_name (ID, Name, Lastname, Email, Password, Isprivate, Isenable) Values ​​(UUID (), '$ Name', '$ LastName', '$ Email', '$ Password', '$ ISPrivate ',' $ iSenable ') ");
                                  Return New WP_REST_RESPONSE (True 200);
                             }
                      }
                   } ELSE {
                       $ response= array ();
                       Return New WP_REST_RESPONSE ($ Response, 401);
                   }
             }
        }
}

Without your class code, no one guesses the cause.

KAGG Design2021-07-15 17:58:11

Updated by adding class code

bugbuster5402021-07-14 21:08:47

The coded code is small. Which hook does the PDBC_CREATE_USER function work? And what caused foo? Usually all manipulations with the REST are produced in the class, which extends WP_REST_CONTROLLER. And here we see a certain hanging class.

KAGG Design2021-07-15 06:44:04

And, by the way, NULL NEW WP_REST_RESPONSE (NULL, 400); Can not return, the title is incorrect. NEW ... always returns a class instance. But what you then do with this object, I would like to see.

KAGG Design2021-07-15 09:36:11