2

I want to design an http route that sets a phone number as main for the current authenticated user.

could you suggest the proper http method and route to use.

I'm hesitating between GET /auth/phones/{phone_id}/main And PATCH /auth/phones/{phone_id} with object {main: true} in body request.

when setting a new main phone old main phone will automatically unset.

3 Answers 3

5

Assuming /auth/phones/{phone_id} represents a particular phone number of the authenticated user, I would do the following:

PATCH /auth/phones/{phone_id}
Content-Type: application/json

{
    "main":true
}

HTTP method GET should not be used to modifiy resource state.

Sign up to request clarification or add additional context in comments.

2 Comments

ya, it seems that PATCH is the more proper method. But here I don't understand why to hit the route /auth instead of auth/phones/{phone_id}. In may case a user could have a collection of phones, each phone with multiple properties (id, user_id, number, main, type(pro, perso), active, ...)
Sorry, I didn't understand that while I read your question. See my edit.
2

According to Mozilla Foundation, PATCH is the correct way to apply partials updates to a record.

Read more in https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/PATCH

Comments

1

Typically, you don't want to modify data via a GET method. The options you should be considering are POST/PUT/PATCH. A typical approach would be:

  • POST : creating a new entity, or sub-entity
  • PUT : replacing an existing entity with the value(s) provided in the request
  • PATCH : partially updating an existing entity

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.