Uncategorized

Drupal 8 REST Requests

In November, 2015, the Stanford Web Services team got to dive into Drupal 8 during a weeklong sprint. I was excited to look at the RESTful web services that Drupal 8 gives out-of-the-box; what follows is my documentation of the various types of requests supported, required headers, responses, and response codes.

This is not intended to be an exhaustive documentation of RESTful web services in Drupal 8. However, I have pulled information from various posts around the Web, and my own experimentation, into this post.

A REST client application (e.g., Paw for Mac OS X) is extremely handy for experimenting with RESTful web services in Drupal 8.

Contents

  1. Resources
  2. GET Request
  3. POST Request
  4. DELETE Request
  5. PATCH Request

Resources

GET Request

The GET request is the simplest, and requires no authentication (if permissions are configured to allow anonymous users to “Access GET on Content resource”). Just send a GET request to the node path and add a _format=hal_json URL parameter.

More documentation available at drupal.org.

Request

<a href="https://example.com/node/<nid>?_format=hal_json“>https://example.com/node/<nid>?_format=hal_json[/geshifilter-code]

Response

200 OK

{
“_links”: {
“self”: {
“href”: “<a href=”https://example.com/node/8?_format=hal_json”
“>https://example.com/node/8?_format=hal_json”
</a>    },
“type”: {
“href”: “<a href=”https://example.com/rest/type/node/article”
“>https://example.com/rest/type/node/article”
</a>    },
“<a href=”https://example.com/rest/relation/node/article/uid”:”>https://example.com/rest/relation/node/article/uid”:</a> [
{
“href”: “<a href=”https://example.com/user/3?_format=hal_json”,
“>https://example.com/user/3?_format=hal_json”,
</a>        “lang”: “en”
}
],
“<a href=”https://example.com/rest/relation/node/article/revision_uid”:”>https://example.com/rest/relation/node/article/revision_uid”:</a> [
{
“href”: “<a href=”https://example.com/user/3?_format=hal_json”
“>https://example.com/user/3?_format=hal_json”
</a>      }
],
“<a href=”https://example.com/rest/relation/node/article/field_tags”:”>https://example.com/rest/relation/node/article/field_tags”:</a> [
{
“href”: “<a href=”https://example.com/taxonomy/term/2?_format=hal_json”,
“>https://example.com/taxonomy/term/2?_format=hal_json”,
</a>        “lang”: “en”
}
] },
“uuid”: [
{
“value”: “a57f21eb-a4fe-4c04-8507-7a9cb1255834”
}
],
“type”: [
{
“target_id”: “article”
}
],
“langcode”: [
{
“value”: “en”,
“lang”: “en”
}
],
“title”: [
{
“value”: “Test Article 2”,
“lang”: “en”
}
],
“_embedded”: {
“<a href=”https://example.com/rest/relation/node/article/uid”:”>https://example.com/rest/relation/node/article/uid”:</a> [
{
“_links”: {
“self”: {
“href”: “<a href=”https://example.com/user/3?_format=hal_json”
“>https://example.com/user/3?_format=hal_json”
</a>          },
“type”: {
“href”: “<a href=”https://example.com/rest/type/user/user”
“>https://example.com/rest/type/user/user”
</a>          }
},
“uuid”: [
{
“value”: “46a5a516-2d00-41fa-b300-f0342bec87f5”
}
],
“lang”: “en”
}
],
“<a href=”https://example.com/rest/relation/node/article/revision_uid”:”>https://example.com/rest/relation/node/article/revision_uid”:</a> [
{
“_links”: {
“self”: {
“href”: “<a href=”https://example.com/user/3?_format=hal_json”
“>https://example.com/user/3?_format=hal_json”
</a>          },
“type”: {
“href”: “<a href=”https://example.com/rest/type/user/user”
“>https://example.com/rest/type/user/user”
</a>          }
},
“uuid”: [
{
“value”: “46a5a516-2d00-41fa-b300-f0342bec87f5”
}
] }
],
“<a href=”https://example.com/rest/relation/node/article/field_tags”:”>https://example.com/rest/relation/node/article/field_tags”:</a> [
{
“_links”: {
“self”: {
“href”: “<a href=”https://example.com/taxonomy/term/2?_format=hal_json”
“>https://example.com/taxonomy/term/2?_format=hal_json”
</a>          },
“type”: {
“href”: “<a href=”https://example.com/rest/type/taxonomy_term/tags”
“>https://example.com/rest/type/taxonomy_term/tags”
</a>          }
},
“uuid”: [
{
“value”: “14bd6eeb-f0f1-4fd1-b200-e27246318b7c”
}
],
“lang”: “en”
}
] },
“status”: [
{
“value”: “1”,
“lang”: “en”
}
],
“created”: [
{
“value”: “1446588457”,
“lang”: “en”
}
],
“changed”: [
{
“value”: “1446589578”,
“lang”: “en”
}
],
“promote”: [
{
“value”: “1”,
“lang”: “en”
}
],
“sticky”: [
{
“value”: “0”,
“lang”: “en”
}
],
“revision_timestamp”: [
{
“value”: “1446588457”
}
],
“revision_translation_affected”: [
{
“value”: “1”,
“lang”: “en”
}
],
“default_langcode”: [
{
“value”: “1”,
“lang”: “en”
}
],
“comment”: [
{
“status”: “2”,
“cid”: “0”,
“last_comment_timestamp”: “1446588457”,
“last_comment_name”: null,
“last_comment_uid”: “3”,
“comment_count”: “0”,
“lang”: “en”
}
] }

Authentication

POST, DELETE and PATCH requests require a CSRF token, as well as HTTP Basic Authentication (with a user’s Drupal user name and password).

Get the token at <a href="https://example.com/rest/session/token“>https://example.com/rest/session/token[/geshifilter-code]

POST Request

Request

<a href="https://example.com/entity/node“>https://example.com/entity/node[/geshifilter-code]

Headers

Header Name Header Value
X-CSRF-Token (token from rest/session/token)
Authorization Basic (hashed username/password)
Content-Type application/hal+json

Body

{
“_links”: {
“type”: {
“href”: “<a href=”https://example.com/rest/type/node/article”
“>https://example.com/rest/type/node/article”
</a>    }
},
“title”: {
“value”: “Test Article 2”
},
“type”: {
“target_id”: “article”
}
}

Response

200 OK

DELETE Request

Request

<a href="https://example.com/node/<nid>“>https://example.com/node/<nid>[/geshifilter-code]

Headers

Header Name Header Value
X-CSRF-Token (token from rest/session/token)
Authorization Basic (hashed username/password)

Response

204 No Content

PATCH Request

Use the PATCH method to update an existing entity.

Request

<a href="https://example.com/node/<nid>“>https://example.com/node/<nid>[/geshifilter-code]

Headers

Header Name Header Value
X-CSRF-Token (token from rest/session/token)
Authorization Basic (hashed username/password)
Content-Type application/hal+json

Body

{
“_links”: {
“type”: {
“href”: “<a href=”https://example.com/rest/type/node/article”
“>https://example.com/rest/type/node/article”
</a>    }
},
“nid”: {
“”: {
“value”: “8”
}
},
“type”: {
“target_id”: “article”
},
“promote”: {
“value”: “1”
},
“body”: {
“”: {
“value”: “<p>foo bar baz</p>”,
“lang”: “en”
}
}
}

Response

204 No Content

 

click here 1
click here 2
click here 3
click here 4
click here 5
click here 6
click here 7
click here 8
click here 9
click here 10
click here 11
click here 12
click here 13
click here 14
click here 15
click here 16
click here 17
click here 18
click here 19
click here 20
click here 21
click here 22
click here 23
click here 24
click here 25
click here 26
click here 27
click here 28
click here 29
click here 30
click here 31
click here 32
click here 33
click here 34
click here 35
click here 36
click here 37
click here 38
click here 39
click here 40
click here 41
click here 42
click here 43
click here 44
click here 45
click here 46
click here 47
click here 48
click here 49
click here 50
click here 51
click here 52
click here 53
click here 54
click here 55
click here 56
click here 57
click here 58
click here 59
click here 60
click here 61
click here 62
click here 63
click here 64
click here 65
click here 66
click here 67
click here 68
click here 69
click here 70
click here 71
click here 72
click here 73
click here 74
click here 75
click here 76
click here 77
click here 78
click here 79
click here 80
click here 81
click here 82
click here 83
click here 84
click here 85
click here 86
click here 87
click here 88
click here 89
click here 90
click here 91
click here 92
click here 93
click here 94
click here 95
click here 96
click here 97
click here 98
click here 99
click here 100
click here 101
click here 102
click here 103
click here 104
click here 105
click here 106
click here 107
click here 108
click here 109
click here 110
click here 111
click here 112
click here 113
click here 114
click here 115
click here 116
click here 117
click here 118
click here 119
click here 120
click here 121
click here 122
click here 123
click here 124
click here 125
click here 126
click here 127
click here 128
click here 129
click here 130
click here 131
click here 132
click here 133
click here 134
click here 135
click here 136
click here 137
click here 138
click here 139
click here 140
click here 141
click here 142
click here 143
click here 144
click here 145
click here 146
click here 147
click here 148
click here 149
click here 150
click here 151
click here 152
click here 153
click here 154
click here 155
click here 156
click here 157
click here 158
click here 159
click here 160
click here 161
click here 162
click here 163
click here 164
click here 165
click here 166
click here 167
click here 168
click here 169
click here 170
click here 171
click here 172
click here 173
click here 174
click here 175
click here 176
click here 177
click here 178
click here 179
click here 180
click here 181
click here 182
click here 183
click here 184
click here 185
click here 186
click here 187
click here 188
click here 189
click here 190
click here 191
click here 192
click here 193
click here 194
click here 195
click here 196
click here 197
click here 198
click here 199
click here 200
click here 201
click here 202
click here 203
click here 204
click here 205
click here 206
click here 207
click here 208
click here 209
click here 210
click here 211
click here 212
click here 213
click here 214
click here 215
click here 216
click here 217
click here 218
click here 219
click here 220
click here 221
click here 222
click here 223
click here 224
click here 225
click here 226
click here 227
click here 228
click here 229
click here 230
click here 231
click here 232
click here 233
click here 234
click here 235
click here 236
click here 237
click here 238
click here 239
click here 240
click here 241
click here 242
click here 243
click here 244
click here 245
click here 246
click here 247
click here 248
click here 249
click here 250
click here 251
click here 252
click here 253
click here 254
click here 255
click here 256
click here 257
click here 258
click here 259
click here 260
click here 261
click here 262
click here 263
click here 264
click here 265
click here 266
click here 267
click here 268
click here 269
click here 270
click here 271
click here 272
click here 273
click here 274
click here 275
click here 276
click here 277
click here 278
click here 279
click here 280
click here 281
click here 282
click here 283
click here 284
click here 285
click here 286
click here 287
click here 288
click here 289
click here 290
click here 291
click here 292
click here 293
click here 294
click here 295
click here 296
click here 297
click here 298
click here 299
click here 300
click here 301
click here 302
click here 303
click here 304
click here 305
click here 306
click here 307
click here 308
click here 309
click here 310
click here 311
click here 312
click here 313
click here 314
click here 315
click here 316
click here 317
click here 318
click here 319
click here 320
click here 321
click here 322
click here 323
click here 324
click here 325
click here 326
click here 327
click here 328
click here 329
click here 330
click here 331
click here 332
click here 333
click here 334
click here 335
click here 336
click here 337
click here 338
click here 339
click here 340
click here 341
click here 342
click here 343
click here 344
click here 345
click here 346
click here 347
click here 348
click here 349
click here 350
click here 351
click here 352
click here 353
click here 354
click here 355
click here 356
click here 357
click here 358
click here 359
click here 360
click here 361
click here 362
click here 363
click here 364
click here 365
click here 366
click here 367
click here 368
click here 369
click here 370
click here 371
click here 372
click here 373
click here 374
click here 375
click here 376
click here 377
click here 378
click here 379
click here 380
click here 381
click here 382
click here 383
click here 384
click here 385
click here 386
click here 387
click here 388
click here 389
click here 390
click here 391
click here 392
click here 393
click here 394
click here 395
click here 396
click here 397
click here 398
click here 399
click here 400
click here 401
click here 402
click here 403
click here 404
click here 405
click here 406
click here 407
click here 408
click here 409
click here 410
click here 411
click here 412
click here 413
click here 414
click here 415
click here 416
click here 417
click here 418
click here 419
click here 420
click here 421
click here 422
click here 423
click here 424
click here 425
click here 426
click here 427
click here 428
click here 429
click here 430
click here 431
click here 432
click here 433
click here 434
click here 435
click here 436
click here 437
click here 438
click here 439
click here 440
click here 441
click here 442
click here 443
click here 444
click here 445
click here 446
click here 447
click here 448
click here 449
click here 450
click here 451
click here 452
click here 453
click here 454
click here 455
click here 456
click here 457
click here 458
click here 459
click here 460
click here 461
click here 462
click here 463
click here 464
click here 465
click here 466
click here 467
click here 468
click here 469
click here 470
click here 471
click here 472
click here 473
click here 474
click here 475
click here 476
click here 477
click here 478
click here 479
click here 480
click here 481
click here 482
click here 483
click here 484
click here 485
click here 486
click here 487
click here 488
click here 489
click here 490
click here 491
click here 492
click here 493
click here 494
click here 495
click here 496
click here 497
click here 498
click here 499
click here 500

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button