mko
mko

Reputation: 7325

How would I update complex object from a single endpoint using REST api?

For example I have an object such as

{
    ID: 1000,
    Type: "Ticket",
    "Followers": [
    {
    "UserID": 1200
    },
    {
    "UserID": 1201
    },
    {
    "UserID": 1202
    }]
}

If I wanted to update this object using a new request, how would a list Followers be treated?

For example if a send this update request using PUT, would it mean that there are no changes in Followers, or that Followers is not empty?

{
    ID: 1000,
    Type: "Ticket",
    "Followers": []
}

Upvotes: 0

Views: 805

Answers (1)

maxime_039
maxime_039

Reputation: 494

I suppose you are designing the API and you are not referring to an existing API.

First I understood that you are talking about a RESTfull API?

Therefore most of the time you will have an endpoint dedicated to update a specific parameter.

For example to update the followers:

PUT/PATCH /api/ticket/1000/followers

Providing an empty array or null value to this request would then mean that you are removing your followers.

Or you can also implement DELETE HTTP method. The idea would then to use PUT only for an update logic - it will then returning an error if you provide and empty/null value.

In that case the only way to delete the followers will be to use the DELETE HTTP method:

DELETE /api/ticket/1000/followers

But once again it will depends of how you want to implement it and what seems the most usable and logical for you and the end users.

Edit: Your question was related to the usage of the single endpoint /api/ticket/1000 without sub resources.

Therefore, sending the following object in PUT without providing "Followers" will not change/delete followers.

{
    ID: 1000,
    Type: "Ticket"
}

At the opposite, providing a key for "Followers" in your request will update the value of the followers. In that case it will delete them:

{
    ID: 1000,
    Type: "Ticket",
    "Followers": []
}

Upvotes: 1

Related Questions