Score:0

REST API calls are always cached

cn flag

In Drupal 9.2.6 I have a caching issue.

The code I am using is the following.

$returnValues = [
  'value' => 'test',
  'time' => time(),
];

$response = new ResourceResponse($returnValues, 200, []);
$disable_cache = new CacheableMetadata();
$disable_cache->setCacheMaxAge(0);
$response->addCacheableDependency($disable_cache);
return $response;

This leads to this error.

TypeError: Argument 1 passed to Symfony\Component\Serializer\Encoder\ChainEncoder::getEncoder() must be of the type string, null given, called in /webspace/public/vendor/symfony/serializer/Encoder/ChainEncoder.php on line 49 in Symfony\Component\Serializer\Encoder\ChainEncoder->getEncoder() (Zeile 80 in /webspace/public/vendor/symfony/serializer/Encoder/ChainEncoder.php)

This is also doesn't work.

$response = new ResourceResponse($returnValues);
$response->addCacheableDependency($returnValues);
return $response;

What's wrong?

Kevin avatar
in flag
So you don't want it cached? But you are adding a cacheable dependency? https://drupal.stackexchange.com/questions/182863/rest-get-without-caching
YusufFidan avatar
cn flag
@apaderno This is also cached: $response = new Response(); $response->setStatusCode(200); $response->setContent(json_encode($returnValues)); return $response;
us flag
This Question likely has some valid Answers: https://drupal.stackexchange.com/q/182863/10645
YusufFidan avatar
cn flag
I don't know why - but today it's working as expected.
Score:0
ph flag

Use Drupal\rest\ModifiedResourceResponse instead of ResourceResponse, then it won't be cached.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.