This is a follow up to my early blog post about enabling RESTful web services in Drupal 8. Jesus Olivas, one of the creators of Drupal Console, brought up that you can actually manage your RESTful endpoint plugin configurations direct from the command line!
If you are not familiar, Drupal Console is a command line utility built for managing Drupal 8. I use it mostly for code generation and some other basic management features, such as debugging the routing system. I talk about Drupal Console it in the "The Drupal CLI" chapter of the Drupal 8 Development Cookbook. However, the tool itself could have a book all on its own for all of its features.
Get Drupal Console
Drupal Console needs to added to your Drupal project, for each project, using Composer. The command is below, and more details in the project documentation: https://docs.drupalconsole.com/en/getting/composer.html
composer require drupal/console:~1.0 \ --prefer-dist \ --optimize-autoloader
Here's the sample output from running the command on the Drupal 8 instance used in my last article.
Review the current state of RESTful endpoints
Listing the currently available REST endpoint plugins we just need to run the following command
$ ./vendor/bin/drupal debug:rest
This will list all of the REST module plugins, their URL endpoints, and status.
Using this command we can also inspect individual plugins. Here's the output from inspecting the Node (content) resource plugin.
./vendor/bin/drupal debug:rest entity:node
Full documentation can be found at https://docs.drupalconsole.com/en/commands/debug-rest.html
We can then use two other commands to enable or disable endpoints.
Enabling a RESTful endpoint using Drupal Console
Now, let's use the command line to enable the Node (content) endpoint.
./vendor/bin/drupal rest:enable entity:node
It will prompt you to select methods that you would like to enable. In this example we'll focus on just enabling the GET resource, for consuming content elsewhere.
$ ./vendor/bin/drupal rest:enable entity:node commands.rest.enable.arguments.methods:  GET  POST  DELETE  PATCH > 0
Then you decide what formats you would like to accept. You can choose between JSON or XML out of the box with Drupal core. Other modules can provide more formats (YAML, CSV.)
$ ./vendor/bin/drupal rest:enable entity:node commands.rest.enable.arguments.methods:  GET  POST  DELETE  PATCH > 0 Selected Method GET commands.rest.enable.arguments.formats:  json  xml > json
And, then we choose an authentication provider. We will choose the cookie authentication provider.
$ ./vendor/bin/drupal rest:enable entity:node commands.rest.enable.arguments.methods:  GET  POST  DELETE  PATCH > 0 Selected Method GET commands.rest.enable.arguments.formats:  json  xml > json commands.rest.enable.messages.selected-format json Available Authentication Providers:  basic_auth  cookie > cookie
And then, success!
Selected Authentication Providers cookie Rest ID "entity:node" enabled
Here's a screenshot of the entire command process.
Modifying a RESTful endpoint using Drupal Console
Now, there is not a command to quick edit the configuration for a RESTful endpoint plugin. To do so, we will have to use the provided configuration management commands to discover the configuration name and edit it. So, we will use the debug:config command to list our options. It's a large output. So I recommend using grep to limit the results.
$ ./vendor/bin/drupal debug:config | grep "rest.resource" rest.resource.entity.node
Now that we have the configuration name, rest.resource.entity.node, we can use the configuration edit command to modify it. Running the command will open a terminal based text editor to modify the configuration YAML.
./vendor/bin/drupal config:edit rest.resource.entity.node
When you save changes, they will be imported. Be warned, however: you could ruin your configuration if you are not sure about what you're doing.
You can rerun the rest:enable command to add new HTTP methods to an endpoint, it seems. However, you will need to edit it to remove. For example, let's say you enabled the DELETE method but want to remove it.