3

Ok i've spent my whole afternoon trying to get this running:

  • Setup a freshly installed Drupal 8

  • Activate the WebServices (HAL, HTTP Basic Auth, Restful Webservices, Serialization)

  • Install REST UI Module

  • Activate the REST Module for e.g. /user/{user} under Configuration -> Web-Services -> REST (i'm activating GET/POST with json and basic_auth)

  • Fix the permissions so all "authenticated user" can access the Restful Webservices for User content

I basically followed this tutorial to get there: https://drupalize.me/blog/201401/introduction-restful-web-services-drupal-8

Then i go forward and create a POST call looking like this:

GET http://{my-host}/user/testing

// with following headers obj
{
  'Authorization': 'Basic dGVzdGluZzptYXN0ZXI=',
  'Content-Type': 'application/json'
}

Well, the user "testing" really exists, i've checked this like 100 times. The Basic Authorization user i'm giving him (i obfuscated the data here since it is still plain text base64) - believe me, it is a correct credential too, even from the admin user.

The path is correct too with /user/{user} - but he is always returning a 404 Not Found ...? Why?

Well what i try to achieve is a login trough a REST API call - so i can login with an existing drupal user from an external system (auth0). As far is i understood from the docs this should all be possible with the /user/{user} endpoint - but i'm totally unsure because i couldn't find any working examples for it.

0

1 Answer 1

0

'Content-Type': 'application/json' needs to be 'Content-Type': 'application/hal+json' in order for it to work.

I recommend you watch these video tutorials

Learn Drupal 8 - REST API Part1

Learn Drupal 8 - REST API Part2

The login is covered in part 2, but part 1 is very informative and part 2 will make more sense if you watch part 1.

Although, the video tutorial uses the Google Chrome Postman add-on, your code needs to do the same.

  1. Have a header with the Content Type and Authorization values

  2. GET http://{my-host}/user/testing

Your URL to POST, GET, or PATCH shouldn't be your user page, unless you want to work with the user fields. The URL needs to have the entity you are working with.

For example: See the url's used in the video tuts.

You can even use Postman to generate your code

HTTP Generation

PATCH /node/1 HTTP/1.1
Host: forexample.com
Content-Type: application/hal+json
Authorization: Basic Q2FtaWxvOmNvY29saXNa
Cache-Control: no-cache // delete
Postman-Token: 3803eeac-0dc2-5c09-05e1-40d15d5b9820 // delete

{
"title": [
{
"value": "The title changed, WOOOHOOO!!!"
}
],
"type": [
{
"target_id": "article"
}
],
"_links": {
"type": {
"href": "http://forexample.com/rest/type/node/article"
}
}
}

PHPV1 generation

<?php

HttpRequest::methodRegister('PATCH');
$request = new HttpRequest();
$request->setUrl('http://forexample.com/node/1');
$request->setMethod(HttpRequest::HTTP_METH_PATCH);

$request->setHeaders(array(
  'postman-token' => '9656f5e8-13a5-0e2b-23cb-5ac03af59b5b', // remove this line
  'cache-control' => 'no-cache', // remove this line 
  'authorization' => 'Basic Q2FtaWxvOmNvY29saXNa',
  'content-type' => 'application/hal+json'
));

$request->setBody('{
"title": [
{
"value": "The title changed, WOOOHOOO!!!"
}
],
"type": [
{
"target_id": "article"
}
],
"_links": {
"type": {
"href": "http://forexample.com/rest/type/node/article"
}
}
}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

You can also generate for other languages like Python, jQuery, PHP Curl to name a few.

3
  • @Pierre.Vriens something came to mind, cheers.
    – No Sssweat
    Commented Apr 27, 2016 at 7:37
  • Approve! ... Except that the "2)"-bullet formatting is still a bit bizarre, but not really something to worry about ... Commented Apr 27, 2016 at 7:52
  • @Pierre.Vriens Unfortunately quotes need to start a the beginning of the line =/
    – No Sssweat
    Commented Apr 27, 2016 at 7:55

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