You are here

function clients_connection_drupal_services_rest_7::getXCSRFToken in Web Service Clients 7.3

Get the X-CSRF token.

This calls the remote site to get the token, and caches it, so that multiple requests made with the same connection don't need to retrieve it again.

This expects the 'user/token' action resource to be enabled on the endpoint. This only exists in Services 3.5 and later. We do not support earlier versions.

Return value

The X-CSRF token.

Throws

An exception if the remote site does not return a token.

1 call to clients_connection_drupal_services_rest_7::getXCSRFToken()
clients_connection_drupal_services_rest_7::makeRequest in connections/clients_drupal_rest/clients_drupal_rest.inc
Make a REST request.

File

connections/clients_drupal_rest/clients_drupal_rest.inc, line 278
Contains classes for Client connections handlers.

Class

clients_connection_drupal_services_rest_7
Class for Drupal client connections, REST D7.

Code

function getXCSRFToken() {
  if (isset($this->CSRFToken)) {
    return $this->CSRFToken;
  }
  $headers = array(
    'Accept' => 'application/json',
    // Pass in the login cookie we received previously.
    'Cookie' => $this->cookie,
  );
  $options = array(
    'headers' => $headers,
    'method' => 'POST',
  );
  $response = drupal_http_request($this->endpoint . '/user/token', $options);
  if ($response->code == 200) {
    $data = json_decode($response->data);
    $this->CSRFToken = $data->token;
    return $this->CSRFToken;
  }
  else {
    throw new Exception(t("Unable to get a CSRF token from the remote site."));
  }
}