0

If I do the following http call to my api on resource /people and return

{
  "_embedded": {
    "people": [
      {
        "id": 1,
        "firstName": "John",
        "lastName": "Doe",
        "_links": {
          "self": {
            "href": "http://localhost:8080/person/1"
          },
          "person": {
            "href": "http://localhost:8080/person/1"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/person"
    }
  }
}

The data is being rendered on screen in a table and currently I expose my id's from my backend and use the following link for my react-router to handle

<Link to={`person/${this.props.person.id}`}>Detail</Link>

Here I can't pass the link in the rendered link and have to pass the id to do the detail call in my backend. But instead of constructing the href manually I want to use the url at _links.self.href in my next component.

How can I achieve this without exposing id's in my React application?

jgeerts
  • 641
  • 8
  • 17

2 Answers2

1

Found a question with the same issue here.

In the Link use:

<Link to={{pathname: "/person", state: { person: person }}} />

And in the referenced component:

this.props.location.state.person
jgeerts
  • 641
  • 8
  • 17
0

Your question is almost identical to the example I give in my answer to this question: https://stackoverflow.com/a/18083285/760706

The main issue I would take with what you have presented is that the resource you have included in the _embedded section (/person/1) is not included in the links of the collection resource, the main _links section. Instead you have listed it next to self within the person's own links.

The second problem I see is that the link relation you have used, "people" (which actually links to an individual person), is not a URI nor on the IANA registered link relations list. These are the only two legal types of link relation. In this instance, your collection should use the registered relation item to point to its members. For custom relations, your URI should be a HTTP URL under a domain you control, pointing to documentation, so that users can look up the documentation for that link relation by going to the URL.

Community
  • 1
  • 1
Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80