I am trying to build simple search autocomplete POST endpoint, that accepts JSON and outputs JSON, but I am having problems getting Drupal 8 to just accept plain JSON.
Currently I am getting a 404 error for that endpoint, even though it is enabled in REST UI and I have permission. If I add serialization_class
with value Drupal\node\Entity\Node
to the class comment, I get 415 error, because it expects a node. Okay, great. How do I actually make it accept JSON? Do I really need to make an Entity type just for my autocomplete search?
Here is the code I have in mymodule/src/Plugin/rest/resource/SearchAutocompleteResource.php.
namespace Drupal\MyModule\Plugin\rest\resource;
use Drupal\rest\Plugin\ResourceBase;
use Drupal\rest\ResourceResponse;
use Drupal\node\Entity\Node;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Provides a resource for search autocomplete.
*
* @RestResource(
* id = "mymodule:searchautocomplete",
* label = @Translation("MyModule: Search Autocomplete"),
* uri_paths = {
* "canonical" = "/api/v1/search-autocomplete",
* "https://www.drupal.org/link-relations/create" = "/api/v1/search-autocomplete"
* }
* )
*/
class SearchAutocompleteResource extends ResourceBase {
public function post($search = null) {
return new ResourceResponse(array(
'example' => 'return value'
));
}
}