I am requesting an article node using Accept: application/json or application/hal+json that has an image and a taxonomy term. The response contains the reference to the term. But there is no reference to the image. The image shows for an anonymous user when view the node. But really, I don't want the reference anyway. I want to get all relevant data to render the image as setup in the Article display settings. I also want the taxonomy term's name(s) and links. I have been search for most of the day now and I can't find how to do it. Is this possible. In version 7 I had done this by an *_alter. Hoping it is easier to get a 'headless' drupal in v8.

I was writing a post on how to use Drupal 8 and AngularJS here: http://www.koberg.com/2014/06/08/headless-drupal-8-and-angularjs-theme-s... but got stuck (scroll to the bottom) at image and term data.

If it is helpful, here is the result of the node request using HAL JSON:

{
  "_links": {
    "self": {
      "href": "http://d8server.local/node/1"
    },
    "type": {
      "href": "http://d8server.local/rest/type/node/article"
    },
    "http://d8server.local/rest/relation/node/article/uid": [
      {
        "href": "http://d8server.local/user/1",
        "lang": "en"
      }
    ],
    "http://d8server.local/rest/relation/node/article/revision_uid": [
      {
        "href": "http://d8server.local/user/1"
      }
    ],
    "http://d8server.local/rest/relation/node/article/field_image": [
      {
        "href": ""
      }
    ],
    "http://d8server.local/rest/relation/node/article/field_tags": [
      {
        "href": "http://d8server.local/taxonomy/term/1"
      }
    ]
  },
  "uuid": [
    {
      "value": "60280c3f-ad19-47a0-9100-0f2dfedfbbbb"
    }
  ],
  "type": [
    {
      "target_id": "article"
    }
  ],
  "langcode": [
    {
      "value": "en"
    }
  ],
  "title": [
    {
      "value": "My First Article",
      "lang": "en"
    }
  ],
  "_embedded": {
    "http://d8server.local/rest/relation/node/article/uid": [
      {
        "_links": {
          "self": {
            "href": "http://d8server.local/user/1"
          },
          "type": {
            "href": "http://d8server.local/rest/type/user/user"
          }
        },
        "uuid": [
          {
            "value": "3d540546-8b33-489d-bc08-8d9028724da8"
          }
        ],
        "lang": "en"
      }
    ],
    "http://d8server.local/rest/relation/node/article/revision_uid": [
      {
        "_links": {
          "self": {
            "href": "http://d8server.local/user/1"
          },
          "type": {
            "href": "http://d8server.local/rest/type/user/user"
          }
        },
        "uuid": [
          {
            "value": "3d540546-8b33-489d-bc08-8d9028724da8"
          }
        ]
      }
    ],
    "http://d8server.local/rest/relation/node/article/field_image": [
      {
        "_links": {
          "self": {
            "href": ""
          },
          "type": {
            "href": "http://d8server.local/rest/type/file/file"
          }
        },
        "uuid": [
          {
            "value": "3d1714b5-9c8e-4378-935c-92318f73ddd5"
          }
        ],
        "uri": [
          {
            "value": "http://d8server.local/"
          }
        ]
      }
    ],
    "http://d8server.local/rest/relation/node/article/field_tags": [
      {
        "_links": {
          "self": {
            "href": "http://d8server.local/taxonomy/term/1"
          },
          "type": {
            "href": "http://d8server.local/rest/type/taxonomy_term/tags"
          }
        },
        "uuid": [
          {
            "value": "24ab1dd3-49e7-4a48-9728-40ca03926369"
          }
        ]
      }
    ]
  },
  "status": [
    {
      "value": "1",
      "lang": "en"
    }
  ],
  "created": [
    {
      "value": "1402097817",
      "lang": "en"
    }
  ],
  "changed": [
    {
      "value": "1402099660",
      "lang": "en"
    }
  ],
  "promote": [
    {
      "value": "1",
      "lang": "en"
    }
  ],
  "sticky": [
    {
      "value": "0",
      "lang": "en"
    }
  ],
  "revision_timestamp": [
    {
      "value": "1402097817"
    }
  ],
  "log": [
    {
      "value": "",
      "lang": "en"
    }
  ],
  "body": [
    {
      "value": "<p>What do we want?</p>\r\n\r\n<blockquote>\r\n<p><span>Let me try to explain to you, what to my taste is characteristic for all intelligent thinking. It is, that one is willing to study in depth an aspect of one&#39;s subject matter in isolation for the sake of its own consistency, all the time knowing that one is occupying oneself only with one of the aspects. We know that a program must be correct and we can study it from that viewpoint only; we also know that it should be efficient and we can study its efficiency on another day, so to speak. In another mood we may ask ourselves whether, and if so: why, the program is desirable. But nothing is gained&nbsp;&mdash;on the contrary!&mdash; by tackling these various aspects simultaneously. It is what I sometimes have called </span><strong>&quot;the separation of concerns&quot;</strong><span>, which, even if not perfectly possible, is yet the only available technique for effective ordering of one&#39;s thoughts, that I know of. This is what I mean by &quot;focusing one&#39;s attention upon some aspect&quot;: it does not mean ignoring the other aspects, it is just doing justice to the fact that from this aspect&#39;s point of view, the other is irrelevant. It is being one- and multiple-track minded simultaneously.</span> - Edsger W. Dijkstra</p>\r\n</blockquote>\r\n",
      "format": "basic_html",
      "summary": ""
    }
  ],
  "comment": [
    {
      "status": "2",
      "cid": "0",
      "last_comment_timestamp": "1402097986",
      "last_comment_name": null,
      "last_comment_uid": "1",
      "comment_count": "0"
    }
  ]
}

Comments

clemens.tolboom’s picture

I guess this is fixed for the image URI. I get both in hal+json and json a populated image field. (the json variant has no useful link to the image as you reported)

curl --user admin:admin --header "Accept: application/json" --request GET http://drupal.d8/node/1
# Needs https://www.drupal.org/node/2310307
curl --user admin:admin --header "Accept: application/json" --request GET http://drupal.d8/entity/file/1

curl --user admin:admin --header "Accept: application/hal+json" --request GET http://drupal.d8/node/1
# Needs https://www.drupal.org/node/2310307
curl --user admin:admin --header "Accept: application/hal+json" --request GET http://drupal.d8/entity/file/1

Have you tried to create a view with a rest display on ie path node/%/full-json .. that probably give you trouble with #2100637: REST views: add special handling for collections

clemens.tolboom’s picture

Issue summary: View changes
Issue tags: +Needs issue summary update

Rechecking the response I get both the image and term links but HAL returns a direct link to the image in its _link and section which should be a link to file. HAL image embed contains link to image but has a type to file.

This feels quite confusing.

@followers please do an issue summary update and some research/tests

json

[{
  target_id: "517",
  alt: "Appellatio erat eros eum humo jus saepius suscipit tincidunt usitas.",
  title: "",
  width: "439",
  height: "134",
  url: "http://drupal.d8:8080/sites/default/files/field/image/generateImage_8JGTxi.jpg"
}],
field_tags: [{
  target_id: "1",
  url: "/taxonomy/term/1"
},
{
  target_id: "2",
  url: "/taxonomy/term/2"
}]

HAL

_links: {
  http://drupal.d8:8080/rest/relation/node/article/field_image: [{
      href: "http://drupal.d8:8080/sites/default/files/field/image/generateImage_8JGTxi.jpg",
      lang: "en"
    }],
    http://drupal.d8:8080/rest/relation/node/article/field_tags: [{
      href: "http://drupal.d8:8080/taxonomy/term/1?_format=hal_json",
      lang: "en"}]
}
...
_embedded: {
  http://drupal.d8:8080/rest/relation/node/article/field_image: [{
    _links: {
      self: {
        href: "http://drupal.d8:8080/sites/default/files/field/image/generateImage_8JGTxi.jpg"
      },
      type: {
        href: "http://drupal.d8:8080/rest/type/file/file"
      }
    },
    uuid: [{
      value: "0a18f086-dce3-4315-9a00-d34429b7d2f4"
    }],
    uri: [{
      value: "http://drupal.d8:8080/sites/default/files/field/image/generateImage_8JGTxi.jpg"
    }],
    lang: "en"
  }],
  http://drupal.d8:8080/rest/relation/node/article/field_tags: [{
    _links: {
      self: {
        href: "http://drupal.d8:8080/taxonomy/term/1?_format=hal_json"
      },
      type: {
        href: "http://drupal.d8:8080/rest/type/taxonomy_term/tags"
      }
    },
    uuid: [{
      value: "d6cc102f-3067-4b4d-8702-2d68b1128014"
    }],
    lang: "en"
  },
Wim Leers’s picture

Status: Active » Fixed

I get both the image and term links but HAL returns a direct link to the image in its _link and section which should be a link to file

That's because File entities do not have canonical URLs, nor edit/delete URLs.

See \Drupal\file\Entity\File::url().

I can confirm #2, but AFAICT that is correct.

clemens.tolboom’s picture

Status: Fixed » Closed (works as designed)

It's not fixed as the requester wanted a styled images which we cannot provide whenever. The rest client must re-query for a styled image ie rewrite the URI http://drupal.d8:8080/sites/default/files/field/image/generateImage_8JGTxi.jpg into the intended image style path.

garphy’s picture

Version: 8.0.x-dev » 9.x-dev
Status: Closed (works as designed) » Active

I don't really get why this was closed.

The OP issue, if I understand correctly, is to gather taxonomy terms information (the label, at least) in the same request that retrieves the node which references theses terms.

I want to get all relevant data to render the image as setup in the Article display settings. I also want the taxonomy term's name(s) and links.

More generally, this is about a way to instruct Drupal to "embed" referenced entities in the same REST response.

I understand that given the features currently provided by core it "works as designed", but can we at least be pointed to a more general follow-up issue or keep this one open to discuss that matter ?

garphy’s picture

Version: 9.x-dev » 8.2.x-dev

And I don't get why it got moved to 9.x :)

clemens.tolboom’s picture

@garphy good point(s).

Maybe #2721489: REST: top priorities for Drupal 8.2.x and #2335229: [meta] REST et al could help finding other issues? From a HAL perspective I would close this issue but from a REST response I agree to get the whole entity content. Problem is what the whole entity should be for REST responses. Do we need a display mode?

Wim Leers’s picture

Status: Active » Closed (works as designed)

@garphy Fair points!

The problem is that all currently supported formats (HAL+JSON, JSON and XML) have normalizations that are just mimicking Drupal's internal data structures 1:1. For that reason, they're great for machine-to-machine synchronization. They're painful for the use case that you describe.

JSON and XML do not support embedding at all.

HAL+JSON does, but prescribes that embedding must happen via links. (There's also an efficiency argument: why keep re-sending the same data as embedded resources with every response if the client can just cache them?)

What you're looking for, is exactly what JSONAPI supports. We intend to bring this to Drupal 8.3 core, see #2757967: API-first initiative. Until then, you can install the JSON API contrib module: https://www.drupal.org/project/jsonapi.

Finally, I think https://github.com/acquia/waterwheel may be able to help take a way a lot of the pain. And I've opened an issue to make this exact thing even less painful: https://github.com/acquia/waterwheel/issues/22.