user9640382
user9640382

Reputation:

Which http method for updating single property

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.

Upvotes: 2

Views: 4972

Answers (3)

manash
manash

Reputation: 7106

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.

Upvotes: 5

DavidA
DavidA

Reputation: 4184

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

Upvotes: 1

LuisMendes535
LuisMendes535

Reputation: 536

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

Upvotes: 2

Related Questions