3

I'm getting the following error on my custom resource when sending the following DELETE request even though POST and GET are working fine

"A fatal error occurred: Serialization for the format is not supported"

The request looks as follows:

enter image description here

If I'd change the method from DELETE to GET it would return status 200 with the intended - and for testing purposes identical - behaviour. The configuration in the Rest UI module looks as the following and the permissions for the requests (GET, POST and DELETE) are allowed for Guests:

Rest UI

Here is the code of my custom class:

/**
 * Provides a Demo Resource
 *
 * @RestResource(
 *   id = "test_resource",
 *   label = @Translation("Demo Resource"),
 *   uri_paths = {
 *      "canonical" = "/test_rest_api/test/{email}",
         "https://www.drupal.org/link-relations/create" = "/test_rest_api/test",
 *   }
 * )
 */
class TestResource extends ResourceBase {
  //Here are construct and create functions

  /**
   * Responds to entity GET requests.
   *
   * Does something
   *
   * @return \Drupal\rest\ResourceResponse
   */
   public function get($email) {
    $message = ["message" => $email];
    return new ResourceResponse($message);
   }

  /**
   * Responds to entity POST requests.
   *
   * Does something
   *
   * @param $data
   * @return \Drupal\rest\ResourceResponse
   */
  public function post(array $data = []) {
    $message = ["message" => $data["email"]];
    return new ResourceResponse($message);
  }

  /**
   * Responds to entity DELETE requests.
   *
   * Does something
   *
   * @return \Drupal\rest\ResourceResponse
   */
  public function delete($email) {
    $message = ["message" => $email];
    return new ResourceResponse($message);
  }
}

Has somebody an idea what I'm overlooking? I disabled/enabled the resource multiple and cleared the cache multiple times

3

In delete method you can not return ResourceResponse. Try return ModifiedResourceResponse.

return new ModifiedResourceResponse(NULL, 204);

Notice: First argument of ModifiedResourceResponse need are NULL, if your specific data for this parameter throw same error.

  • Your method also works with the normal ResourceResponse, but according to api.drupal.org/api/drupal/… ModifiedResourceResponse is the way to go. – FeBe Oct 8 '17 at 10:43
0

Also in DELETE method if you want to return a response you could use return new JsonResponse('Processed'); including the following use statement: use Symfony\Component\HttpFoundation\JsonResponse;

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.