WP_REST_Request always outputs an error object with rest_forbidden
-
I am using the rest_do_request() method with the WP_REST_Request API to trigger the creation of a redirect in my PHP code.
The creation of the redirect is successful, however the response always has the status with a value of 401, and data array block has the code value as ‘rest_forbidden’, etc.
I didn’t think that there was any authentication required when using the rest_do_request() method. So why does the rest response always say forbidden regardless?
This is the create redirection code block I am using (but this also happens for deletes with bulk and updates, etc) with valid values for the $source, $target, and $group_id, and as mentioned the redirect is successfully created:
$request = new WP_REST_Request('POST', '/redirection/v1/redirect'); $request->set_query_params([ 'url' => $source, 'action_data' => [ 'url' => $target, ], 'regex' => 0, 'group_id' => $group_id, 'match_type' => 'url', 'action_type' => 'url', 'action_code' => $action_code, ]); $response = rest_do_request($request);
What could be the problem here?
- The topic ‘WP_REST_Request always outputs an error object with rest_forbidden’ is closed to new replies.