manniru
11/18/2016 - 8:20 AM

Drupal 8 REST

Drupal 8 REST

ACCESS REST CSRF TOKEN

rest/session/token

HEADERS REQUIRED FOR POST | PATCH | DELETE

Authorization(username:password encoded)
X-CSRF-Token(token)
Content-Type(=hal+json) - The response type format. Try use hal+json

User should also have the right permission to do the operation

GET NODE

**GET** http://phenix.mda-geneve.ch/node/12?_format=hal_json

POST NODE

**POST** http://phenix.mda-geneve.ch/entity/node
{
  "type":[{"target_id":"article"}],
  "title":[{"value":"Hello World"}],
  "body":[{"value":"How are you?"}]
}

UPDATE NODE

**PATCH** http://phenix.mda-geneve.ch/node/id
{
  "nid":[{"value":"15"}],
  "type":[{"target_id":"article"}],
  "title":[{"value":"Goodbye World"}]
}

GET PROFILE

**GET** http://phenix.mda-geneve.ch/profile/2?_format=hal_json

POST PROFILE

**POST** http://phenix.mda-geneve.ch/entity/profile
{
  "type":[{"target_id":"membre"}],
  "field_civilite":[{"value":"Monsieur"}],
  "field_nom":[{"value":"Cyril"}],
  "field_prenom":[{"value":"BOISSENIN"}],
  "uid":[{"target_id": 1}]
}

GET LINE ITEM

**GET** http://phenix.mda-geneve.ch/admin/structure/eck/entity/line_item/1?_format=hal_json

PATCH INVOICE

**PATCH** http://phenix.mda-geneve.ch/admin/structure/eck/entity/line_item/1

REQUEST BODY

{
  "_links": {
    "type": {
      "href": "http://phenix.drupalvm.dev/rest/type/line_item/simple"
    }
  },
  "field_status": [
    {
      "value": "unpaid"
    }
  ]
}

GET INVOICE

**GET** http://phenix.mda-geneve.ch/admin/structure/eck/entity/invoice/1?_format=hal_json

POST INVOICE

**POST** http://phenix.mda-geneve.ch/entity/invoice

REQUEST BODY

{
  "_links": {
    "type": {
      "href": "http://phenix.mda-geneve.ch/rest/type/invoice/member"
    }
  },
  "type": [
  { "target_id": "member" }
  ],
    "field_invoice_model": [
  { "value": "membership" }
  ],
  "title": [
    {
      "value": "Invoice title ",
      "lang": "fr"
    }
  ]
}

Additional links and ressources for future implementations :

https://www.chapterthree.com/blog/custom-restful-api-drupal-8

https://www.drupal.org/docs/8/api/restful-web-services-api/restful-web-services-api-overview

  1. Enable core REST module

  2. Install REST UI

https://www.drupal.org/project/restui
  1. Enable ressources with method and permissions