-
Notifications
You must be signed in to change notification settings - Fork 22
Description
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