1

I have a http request to WP REST API that can resolve in a success or a failure status

The success gets handled by WP_REST_Response, and if any error occurs this is handled by WP_Error, both returned if that's the case

However, in both cases when I receive the server response I don't get any of the messages I've added

    public function myFunction($request = null)
    {
        $response = array();
        $parameters = $request->get_json_params();
        $error = new WP_Error();

        if (success) {
                $response['code'] = 200;
                $response['message'] = __("Great", "great");
            } else {
                $error->add(406, __("nope", 'almost'), array('status' => 400));
                return $error;
            }

        return new WP_REST_Response($response, 200);
    }

If I inspect the response that I receive, I cannot have access to any message, I only receive

Response {
  body: (...)
  bodyUsed: true
  headers: Headers {}
  ok: true
  redirected: false
  status: 200
  statusText: "OK"
  url: "..."
  __proto__: Response
}

How does it work?

1

Already found, if I parse the response response.json() I get the messages in both cases

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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