Problem/Motivation
When using the hal_json
format with an entity that has a file
field the hal_json
output on GET
does not provide enough information to access the file
entity via the REST API.
I discovered this while reviewing #1927648: Allow creation of file entities from binary data via REST requests
I wanted to GET a node with a file field then be able to either GET, POST, PATCH or DELETE the file referenced. The example was working with had both a image and file field.
Here is the node GET response, the example has an image
field, a public://
file
field and a private://
file
field.
{
"_links": {
"self": {
"href": "http://d8.dev/node/37?_format=hal_json"
},
"type": {
"href": "http://d8.dev/rest/type/node/article"
},
"http://d8.dev/rest/relation/node/article/revision_uid": [
{
"href": "http://d8.dev/user/1?_format=hal_json"
}
],
"http://d8.dev/rest/relation/node/article/uid": [
{
"href": "http://d8.dev/user/1?_format=hal_json",
"lang": "en"
}
],
"http://d8.dev/rest/relation/node/article/field_image": [
{
"href": "http://d8.dev/sites/default/files/2017-09/webform1.png",
"lang": "en"
}
],
"http://d8.dev/rest/relation/node/article/field_private_file": [
{
"href": "http://d8.dev/system/files/2017-09/interdiff-109-115.txt"
}
],
"http://d8.dev/rest/relation/node/article/field_public_file": [
{
"href": "http://d8.dev/sites/default/files/2017-09/interdiff-275-277.txt"
}
],
"http://d8.dev/rest/relation/node/article/field_tags": [
{
"href": "http://d8.dev/taxonomy/term/1?_format=hal_json",
"lang": "en"
}
]
},
"nid": [
{
"value": 37
}
],
"uuid": [
{
"value": "aef1528d-aedb-4afc-a20a-755a71805937"
}
],
"vid": [
{
"value": 53
}
],
"langcode": [
{
"value": "en",
"lang": "en"
}
],
"type": [
{
"target_id": "article"
}
],
"revision_timestamp": [
{
"value": "2017-09-07T19:33:55+00:00",
"format": "Y-m-d\\TH:i:sP"
}
],
"_embedded": {
"http://d8.dev/rest/relation/node/article/revision_uid": [
{
"_links": {
"self": {
"href": "http://d8.dev/user/1?_format=hal_json"
},
"type": {
"href": "http://d8.dev/rest/type/user/user"
}
},
"uuid": [
{
"value": "3d76e74e-25b5-45e9-af89-3d94f253b46c"
}
]
}
],
"http://d8.dev/rest/relation/node/article/uid": [
{
"_links": {
"self": {
"href": "http://d8.dev/user/1?_format=hal_json"
},
"type": {
"href": "http://d8.dev/rest/type/user/user"
}
},
"uuid": [
{
"value": "3d76e74e-25b5-45e9-af89-3d94f253b46c"
}
],
"lang": "en"
}
],
"http://d8.dev/rest/relation/node/article/field_image": [
{
"_links": {
"self": {
"href": "http://d8.dev/sites/default/files/2017-09/webform1.png"
},
"type": {
"href": "http://d8.dev/rest/type/file/file"
}
},
"uuid": [
{
"value": "ce48df6b-83e9-47b8-9875-c2fe391ef9b1"
}
],
"uri": [
{
"value": "http://d8.dev/sites/default/files/2017-09/webform1.png"
}
],
"lang": "en"
}
],
"http://d8.dev/rest/relation/node/article/field_private_file": [
{
"_links": {
"self": {
"href": "http://d8.dev/system/files/2017-09/interdiff-109-115.txt"
},
"type": {
"href": "http://d8.dev/rest/type/file/file"
}
},
"uuid": [
{
"value": "50ab6ba6-7a86-4027-a86d-a26ebf0e09b5"
}
],
"uri": [
{
"value": "http://d8.dev/system/files/2017-09/interdiff-109-115.txt"
}
]
}
],
"http://d8.dev/rest/relation/node/article/field_public_file": [
{
"_links": {
"self": {
"href": "http://d8.dev/sites/default/files/2017-09/interdiff-275-277.txt"
},
"type": {
"href": "http://d8.dev/rest/type/file/file"
}
},
"uuid": [
{
"value": "bb9ed1d1-f613-4647-b932-2e2f52143bfd"
}
],
"uri": [
{
"value": "http://d8.dev/sites/default/files/2017-09/interdiff-275-277.txt"
}
]
}
],
"http://d8.dev/rest/relation/node/article/field_tags": [
{
"_links": {
"self": {
"href": "http://d8.dev/taxonomy/term/1?_format=hal_json"
},
"type": {
"href": "http://d8.dev/rest/type/taxonomy_term/tags"
}
},
"uuid": [
{
"value": "5228c13c-265a-42d1-bbca-ea4a30385cfc"
}
],
"lang": "en"
}
]
},
"status": [
{
"value": true,
"lang": "en"
}
],
"title": [
{
"value": "uuuuu",
"lang": "en"
}
],
"created": [
{
"value": "2017-09-06T14:22:27+00:00",
"lang": "en",
"format": "Y-m-d\\TH:i:sP"
}
],
"changed": [
{
"value": "2017-09-07T19:33:55+00:00",
"lang": "en",
"format": "Y-m-d\\TH:i:sP"
}
],
"promote": [
{
"value": true,
"lang": "en"
}
],
"sticky": [
{
"value": false,
"lang": "en"
}
],
"default_langcode": [
{
"value": true,
"lang": "en"
}
],
"revision_translation_affected": [
{
"value": true,
"lang": "en"
}
],
"path": [
{
"alias": null,
"pid": null,
"langcode": "en",
"lang": "en"
}
],
"content_translation_source": [
{
"value": "und",
"lang": "en"
}
],
"content_translation_outdated": [
{
"value": false,
"lang": "en"
}
],
"comment": [
{
"status": 2,
"cid": 0,
"last_comment_timestamp": 1504707773,
"last_comment_name": null,
"last_comment_uid": 1,
"comment_count": 0,
"lang": "en"
}
]
}
Nowhere in the response do I have access to the file
entity's id or url to access the file
entity via the REST API.
This not true for the other entity reference fields.
For the term I am given: "href": "http://d8.dev/taxonomy/term/1?_format=hal_json
and for the user I am given: "href": "http://d8.dev/taxonomy/term/1?_format=hal_json
Therefore I wanted to view, update or delete the file entity I would not be able to via REST.
Proposed resolution
Not sure but somehow return http://d8.dev/entity/file/1?_format=hal_json
Remaining tasks
User interface changes
API changes
Response for hal_json
normalized file fields would be changed.
Data model changes