0

Giving this naming convention:

http://www.restapitutorial.com/lessons/restfulresourcenaming.html

for the POST (insert) the url of the resource should follow this path/logic:

 http://www.example.com/products/X123
 {
       "color":"something"
 }

Is the following path conceptually wrong? and why is it correct/wrong?

 http://www.example.com/products
 {
       "id":"X123"
       "color":"something"
 }

the ID is generated externally

Also for the PUT is it ok to apply the same logic? (the id naturally must not be changed but used only as ref)

Thank you

MkM
  • 201
  • 4
  • 11

2 Answers2

2

For POST (which is usually used to create a new item in collection) use the following:

http://www.example.com/products
{
   "color":"something"
}

if you have a requirement where client generated the id, the it is

http://www.example.com/products
{
   "id": "abc123"
   "color":"something"
}

EDIT:

For PUT it should be:

http://www.example.com/products/abc123
{
   "color":"something else"
}
Zepplock
  • 28,655
  • 4
  • 35
  • 50
  • What about the PUT? do you think it's correct to add the _id_ in the url or in the body of the request? – MkM Mar 22 '17 at 09:59
0

In my opinion your scenario suits best for PUT method, I would always prefer to include id in url if I only know it - that would be the clearest solution for others.

Second part about including id in body, there is nice answer about that: https://stackoverflow.com/a/28108844/3301697 The only thing I would change in this answer is to include id to every PUT request, if you know it, why hide it.

Community
  • 1
  • 1
Rychu
  • 86
  • 1
  • 7