Support » Fixing WordPress » REST API – How to send custom HTTP-Status?

  • Hello,

    I’m currently working on a plugin.
    I make Ajax-Calls through build-in REST API. (I don’t use the extra REST API plugin)

    My JS makes an Ajax-call (POST) to the REST API route and my custom PHP-script is processing the request.
    My PHP-script fetches data from the database and sends it back to the browser.

    Its working fine, but I want to implement some better error-handling.
    Is it possible to send a custom HTTP-Status to the browser?
    For example if my sql-query doesn’t return any data, I want to send a useable HTTP-Status code.

    I tried it with for example with the status_header() function, but this doesn’t work.
    How it has to be done?

    Thank you

Viewing 3 replies - 1 through 3 (of 3 total)
  • You can use the status_header() function in WordPress, Function Reference / Status Header

    What do you mean by custom code?
    [StackOverflow Thread] You can return custom 4XX error codes (preferably those that are unassigned) for your own application’s error conditions – that is, 2xx for success, 4xx for Client error, etc
    List of HTTP Status Codes

    Can you explain a little better what your end goal is?

    Here is a useful article for HTTP Status Codes – RFC 2616

    HTTP status codes are extensible. HTTP applications are not required to understand the meaning of all registered status codes, though such understanding is obviously desirable. However, applications MUST understand the class of any status code, as indicated by the first digit, and treat any unrecognized response as being equivalent to the x00 status code of that class, with the exception that an unrecognized response MUST NOT be cached. For example, if an unrecognized status code of 431 is received by the client, it can safely assume that there was something wrong with its request and treat the response as if it had received a 400 status code.

    • This reply was modified 5 years, 1 month ago by James. Reason: Updated content
    Thread Starter walhallax

    (@walhallax)

    Hi,

    What do you mean by custom code?

    I will try to explain …

    I have registered the following route, which calls the function “ajax_Interface”:

    register_rest_route( 'blabla/v1', '/ajax-interface', array(
    	'methods' => WP_REST_Server::CREATABLE,
    	'callback' => array( $this,'ajax_interface' ),
    	'permission_callback' => function () {
    	  return current_user_can( 'administrator' );
    	}
    ));

    The function “ajax_interface” does some work. For example I do a SQL-update …

    function ajax_interface() {
    
            $db_update_result = $wpdb->update( ... SQL update ...
    
            if ( $db_update_result > 0) {
    
               error! - Give error status code back to JS
    
            } else {
            
               success!
    
            }
    
    	return ( $ajax_callback_array );
    
    }

    If the SQL-update is not OK it should give a status code back. I want to react on error with JS:

    error: function(data) { 
    	// if error occured
    }

    As mentioned the function “status_header()” seems not to be working with REST API like above. I used it with admin-ajax before. Are you sure it is working? Have you done it already?

    Thank you

    Bump

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘REST API – How to send custom HTTP-Status?’ is closed to new replies.