1

i'm playing with the REST api and when doing some REST requests I got sometimes what I want, and sometimes this response generated by rest_ensure_response() :

{
    "code": "rest_no_route",
    "data": {
        "status": 404
    },
    "message": "No route was found matching the URL and request method"
}

...But how do you deal with that response, since it is returned as a simple JSON object (or array). It is not a WP_Error object that I could check.

Here's how I do actually :

function api_request($url){

    $request = wp_remote_get($url);
    if (is_wp_error($request)) return $request;

    $response = wp_remote_retrieve_body( $request );
    if (is_wp_error($response)) return $response;

    $response = json_decode($response, true);

    //handle errors ?
    if ( $code = wpsstm_get_array_value('code',$response) ){
        $message = wpsstm_get_array_value('message',$response);
        $data = wpsstm_get_array_value('data',$response);
        $error = new WP_Error($code,$message,$data );

        $this->debug_log($error,'query API error');

        return $error;
    }

    return $response;

}

Thanks !

5
  • Well, what's your code for making the API request? And what exactly do you mean by, "handle"?
    – Sally CJ
    Commented Mar 21, 2019 at 2:12
  • @SallyCJ : I just edited te question
    – gordie
    Commented Mar 21, 2019 at 6:28
  • I think you are already dealing properly with the response/error - when the response body is a JSON object with a code, message and data.status other than 400, you can assume the REST API returned a WP_Error but of course as a string.
    – Sally CJ
    Commented Mar 21, 2019 at 22:40
  • @SallyCJ, yes, that is it. So I can rely on code + message + data.status ?
    – gordie
    Commented Mar 22, 2019 at 8:45
  • Yes, you can, if the REST API endpoint uses rest_ensure_response() to return the response. So you could do something like if ( response.data && response.data.status >= 400 ) to check if it's an error. Btw, sorry about the "other than 400" in my previous comment... I actually meant to say, "equals to or greater than 400".
    – Sally CJ
    Commented Mar 23, 2019 at 6:10

1 Answer 1

1

I dug in the codex and found some interesting functions. This is how I finally did it :

function api_request($api_url = null, $params=null,$method = 'GET'){

    if (!$api_url){
        return new WP_Error('no_api_url',"Missing API URL");
    }

    //Create request
    $request = WP_REST_Request::from_url( $api_url );

    //Method
    $request->set_method( $method );

    //params
    switch($method){
        case 'GET':
            $request->set_query_params($params);
        break;
        case 'POST':
            $request->set_body_params($params);
        break;
    }

    //Get response
    $response = rest_do_request( $request );

    if ( $response->is_error() ) {
        return $response->as_error();
    }

    //Get datas
    $datas = $response->get_data();

    return $datas;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.