I'm implementing a new REST API. In this API I typically POST to collections to create resources. For one of my resources, the ID is known before it is created. Does it make more sense to post to the collection with the ID in the body or to post to the instance (as yet non-existent) with the ID in the URL?
Asked
Active
Viewed 131 times
3 Answers
1
I'd say keep the existing endpoint and just add the ID
in the body when POST
ing to that collection, there's no point in adding a new separate route for what's basically the same thing.

Alexandru Marculescu
- 5,569
- 6
- 34
- 50
1
From another thread: https://stackoverflow.com/a/18474955/1851581
You can also use PUT
with the ID
in the URL, instead of POST.
PUT
is used for upserting, covering mainly updates but also the creation of the resource if it doesn't exist.

rruimor
- 11
- 4
0
I ended up posting to the collection with the ID in the body. That keeps with the pattern elsewhere in my API. The only difference here is that the ID is supplied in the body.

loweryjk
- 164
- 1
- 7