REST GET without caching Using this tutorial Drupal Console's drupal gprr command, I've created a custom REST resource in Drupal 8. I'm able to use this resource to retrieve a simple JSON object that says hello to the current user by doing a GET on:

http://localhost/example/hello?_format=json


The JSON returned then looks like this:

{ "hello": "tyler" }


This is to be expected, as I'm logged in. But when I logout, and do another GET on the resource, the same result is returned:

{ "hello": "tyler" }


Doing a full Drupal cache clear fixes this issue, but I'd like to specifically disable caching on this resource. How can I do that?

As a workaround, I thought adding a time stamp to the query string would work:

http://localhost/example/hello?_format=json&amp;time=123456789


But that also returns the exact same result, no matter what time stamp value is in the query string.

For brevity, here's the skeleton code for building the GET resource:

class ExampleHello extends ResourceBase {

  /**
   * {@inheritdoc}
   */
  public static function create(/* ... */) {
    return new static(/* ... */);
  }

  /**
   * Constructs a Drupal\rest\Plugin\ResourceBase object.
   */
  public function __construct(/* ... */) {
    parent::__construct(/* ... */);
    // ...
  }

  /*
   * Responds to GET requests.
   */
  public function get() {

    $account = \Drupal::currentUser()-&gt;getAccount();
    if (!$account-&gt;id()) {
      return new ResourceResponse(array(
        'welcome' =&gt; 'visitor'
      ));
    }
    return new ResourceResponse(array(
      'hello' =&gt; $account-&gt;getAccountName()
    ));
  }

}


Is there a configuration/setting I need to set on this plugin to disable caching on GET requests?
You want to read the Cacheability documentation. it says render arrays, but it also applies to response objects.

ResourceResponse implements CacheableResponseInterface, which has the addCacheableDependency method.

If in any way possible, you want to avoid disabling the cache (which you would do by setting the cache max age to 0), but add the necessary cache contexts and cache tags.

In your cache, that means all you need to do is $response->addCacheableDependency($account).

You also don't need getAccount(), just use currentUser() directly, it's a proxy.