1

I develop a custom module to store and retrieve users data from an API. I used the user entity hooks to call the API on registration, edition and consultation of the Drupal users. It works well, and we use it on several website all using the same partner API.

To update or save the data, we use a POST method with the fields in the body and without URL parameters.

In some cases, when 2 users are registering or saving their account at the same time (max 3 seconds delay), then the information return in the POST response of the second call is the same as the first one.

*** Edit *** I forget to say, after each POST the page is reloaded and their is a GET on the API using the id_contact that I store on the user data (states). I'm not sure if this is relevant. *** /Edit ***

Here is the method I'm using to call the API:

/**
 * Make the Calls to the API.
 *
 * @param string $function
 *   The API endpoint to call.
 * @param string $method
 *   The method (GET / POST / ...).
 * @param array $data
 *   The data to POST to the API.
 *
 * @return array
 *   Array of data from the API, can be empty if error or no results.
 */
public static function callApi($function, $method = 'GET', array $data = []) {
  $srv_url = 'https://someapi.com/';

  if ($srv_url === '/') {
    return FALSE;
  }

  $headers = [
    'Content-Type' => 'application/json;charset=UTF-8',
  ];

  $token = self::getToken($srv_url);
  if ($token === FALSE) {
    return FALSE;
  }
  elseif ($token !== TRUE) {
    $headers['Authorization'] = 'Bearer ' . $token;
  }

  $options = [
    'headers' => $headers,
  ];
  $full_url = $srv_url . $function;

  if ($method === 'POST') {
    $options['body'] = Json::encode($data);
  }
  else {
    $full_url .= '?' . UrlHelper::buildQuery($data);
  }

  try {
    $client = \Drupal::httpClient();
    $mthd = strtolower($method);
    $response = $client->$mthd($full_url, $options);

    $statusCode = $response->getStatusCode();
    // Expected result.
    $jresp = Json::decode($response->getBody());

    // No result (GET).
    if ($statusCode == 204) {
      return [];
    }

    if (isset($jresp['code']) || !in_array($statusCode, [200, 201])) {
      // Something went wrong.
      $code = isset($jresp['code']) ? $jresp['code'] : $statusCode;
      if (isset($jresp['error'])) {
        $msg = $jresp['error'];
      }
      elseif (isset($jresp['message'])) {
        $msg = $jresp['message'];
      }
      else {
        $code = $statusCode;
        $msg = $response->getReasonPhrase() . '<br>' . $response->getBody()->getContents();
      }
      $message = "Response API {$method} - {$function} : {$code}<br>{$msg}<pre>" . print_r($response, TRUE) . '</pre>';
      \Drupal::logger('mymodule')->error($message);
      return [];
    }

    // Log posted result.
    if ($method === 'POST' && $function === 'contact') {
      AtixTools::debug('atix_crm_cible', ["Result API {$method} - {$function}" => ['options' => $options, 'resp' => $jresp]]);
    }
    if (isset($jresp['body'])) {
      return $jresp['body'];
    }
    return $jresp;
  }
  catch (RequestException $e) {
    watchdog_exception('mymodule', $e);
  }

  return [];
}

Here are an example of two calls where the data have been mixed. In the arrays are the options used for the POST request and the Body answered by the API.

Array
(
    [options] => Array
        (
            [headers] => Array
                (
                    [Content-Type] => application/json;charset=UTF-8
                    [Cache-Control] => no-cache
                    [Authorization] => Bearer XXXXXXX
                )

            [body] => @"email":[email protected],"sms":"","nom":"ttf-test54","prenom":"ttf-test54","date_de_naissance":"","code_postal":"","ville":"","langue":"fr","id_filiale":9
        )

    [resp] => Array
        (
            [0] => Array
                (
                    [langue] => fr
                    [nom] => ttf-test54
                    [upd_date] => 2021-08-26 14:18:22
                    [id_contact] => 53550
                    [prenom] => ttf-test54
                    [email] => [email protected]
                )
        )
)

The second one has the same answer

Array
(
    [options] => Array
        (
            [headers] => Array
                (
                    [Content-Type] => application/json;charset=UTF-8
                    [Cache-Control] => no-cache
                    [Authorization] => Bearer XXXXXXX
                )

            [body] => @"email":[email protected],"sms":"","nom":"ttf-test55","prenom":"ttf-test55","date_de_naissance":"","code_postal":"","ville":"","langue":"fr","id_filiale":9
        )

    [resp] => Array
        (
            [0] => Array
                (
                    [langue] => fr
                    [nom] => ttf-test54
                    [upd_date] => 2021-08-26 14:18:22
                    [id_contact] => 53550
                    [prenom] => ttf-test54
                    [email] => [email protected]
                )
        )
)

The people from the API assure me their is no cache on the server side, specially for a POST. The API is developed just for this purpose and is hosted on AWS (Amazon).

How is it possible that 2 calls to the API using Guzzle from two web pages are giving back the same set of data ? Is their a way Drupal or Guzzle are caching the response from the server ?

I looked a lot to find a solution to resolve this trouble, try using no cache header (like in this example), try reproducing it with Postman or a scenario in integromat without any success.

Thanks for some help.

*** Edit2 ***

I had confirmation some data are mixed between 2 sites, which means it must come from the server side and the provider is working on a fix.

I choose to select the answer of @Yuseferi as good, even if it is not the case here, because it could have improve my code and I learn about ResourceResponse.

2
  • Sounds like Drupal is caching the Response.
    – Kevin
    Aug 27, 2021 at 16:19
  • It's not sure after all, I just cross some data of the API database, and some of the contact id we store in drupal are mixed also between sites. It's mean it must be the API server responding 2 times the same data using cache. I'm waiting for confirmation.
    – TytooF
    Aug 27, 2021 at 19:41

1 Answer 1

1

I think it's drupal cache,

instead of return $jresp;

try

$build = array(
  '#cache' => array(
    'max-age' => 0,
  ),
);

return (new ResourceResponse($jresp))->addCacheableDependency($build);

or

return new ModifiedResourceResponse($jresp, 200);

note: of course do not forget to add this classed in use section ;)

4
  • I don't use render on this method, it is used on a form submit call, and I was thinking caching apply to rendered elements, no ?
    – TytooF
    Aug 27, 2021 at 19:38
  • I didn't knew ResourceResponse, it's seems interesting and I'm going to try.
    – TytooF
    Aug 27, 2021 at 20:01
  • @TytooF did you try my solutions? I think the problem is caching.
    – Yuseferi
    Aug 28, 2021 at 20:05
  • No I didn't try the proposed solution, I had confirmation that the trouble was part of the server side API.
    – TytooF
    Sep 2, 2021 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.