samedi 1 février 2020

Rest API Design: Why are path variables favored over the body

I've been having a discussion about REST API design at work and I'm hoping someone can answer a question for me.

Most of what I've read on REST API design best practices seem to agree that when doing a PUT, PATCH, or DELETE you should use the URI to identify the resource, and the body to pass any additional data.

So, in a route where you are making an update to the email of user 123, you would have something like this:

PUT /users/123

body:
{
  email: "some_new_email@email.com"
}

My question is why is it favored over something like this:

PUT /users

body:
{
  userId: 123,
  email: "some_new_email@email.com"
}

Pretty much everything I've read seems to agree that the first example is considered the best practice, but I've yet to find anything that explains why that is other than keeping it consistent with the GET requests. I'm not trying to argue against the best practices, just trying to understand why that is favored.

Aucun commentaire:

Enregistrer un commentaire