Skip to content

Wrong self link in POST response #101

Description

@bfoerster

I'm currently playing a little with this plugin and found an issue with POST requests.

If I POST a resource to my server the response has HAL format.
But unfortunately the self link does not point to the created resource. Am I missing something?

My route implementation looks like this:

{
    method: 'POST',
    path: '/pets',

    config: {
        handler: (request, reply) => {
            const body = request.payload;
            body.id = randomId();
            allPets.push(body);
            return reply(body);
        },
        description: 'POST a new Pet',
        notes: 'Creates a new pet',
        tags: ['api'],
        plugins: {
            hal: {
                ignore: 'id',
            }
        }
    }
}

If I make a POST request
curl -X POST --header 'Accept: application/hal+json' --header 'Content-Type: application/vnd.api+json' -d '{"name":"Bodo","category":"Dog"}' http://localhost:8000/pets
I get the following response:

{
  "_links": {
    "self": {
      "href": "/pets"
    }
  },
  "name": "Bodo",
  "category": "Dog"
}

I think the self link should contain the id of the resource:

{
  "_links": {
    "self": {
      "href": "/pets/pet_id"
    }
  },
  "name": "Bodo",
  "category": "Dog"
}

My workaround is to add the id to the link in the prepare function:

{
    method: 'POST',
    path: '/pets',

    config: {
        handler: (request, reply) => {
            const body = request.payload;
            body.id = randomId();
            allPets.push(body);
            return reply(body);
        },
        description: 'POST a new Pet',
        notes: 'Creates a new pet',
        tags: ['api'],
        plugins: {
            hal: {
                ignore: 'id',
                prepare: (rep, done) => {
                    rep._links.self.href = '/pets/' + rep.entity.id;
                    done();
                }
            }
        }
    }
}

You can see my example "application" here
https://github.com/bfoerster/nodejs-hapi-hal/blob/master/src/routes/pets.js

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions