Skip to content

Instantly share code, notes, and snippets.

@mjordan
Created November 22, 2018 16:44
Show Gist options
  • Save mjordan/6368d0d07047528d85b2b1dd7a997b10 to your computer and use it in GitHub Desktop.
Save mjordan/6368d0d07047528d85b2b1dd7a997b10 to your computer and use it in GitHub Desktop.
Using JWT for internal REST calls in Drupal 8.
// Construct Authorization header using jwt token.
$container = \Drupal::getContainer();
$jwt = $container->get('jwt.authentication.jwt');
$auth = 'Bearer ' . $jwt->generateToken();
$client = \Drupal::httpClient();
$options = [
'auth' => [],
'headers' => ['Authorization' => $auth],
'form_params' => []
];
$response = $client->request('GET', 'http://localhost:8000/node/' . $nid . '/media?_format=json', $options);
@Eventusrus
Copy link

Thank you!! You save me a lot of days! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment