bijiDango
bijiDango

Reputation: 1596

What does RESTful API format '/foos/1/bars/2' means?

I am reading on RESTful API, and this confused me.

DELETE /zoos/12/animals/5 - Deletes animal #5 for zoo #12

Why do anyone need to imply what zoo it is when you know the animal id? Won't just DELETE /animals/5do the job?

Or is this statement means to delete the 5th animal for zoo #12?

Upvotes: 0

Views: 53

Answers (3)

Matt
Matt

Reputation: 879

Using the following request, I will explain this RESTful request:

DELETE /zoos/12/animals/5

The DELETE, defines the method for the REST Request. In this case, we are requesting that we remove/delete a resource.

The /zoos/12/animals/5 is the resource that we are making the request on. You can think of this as a Unique ID, but not for a single entity in a database. It is a logical entity that may span multiple tables in a traditional relational database.

This specific REST URI suggests that we are selecting a Zoo from Zoos with ID 12 and an Animal from Animals with ID 5, although REST doesn't mean that the backend implementation is Relational.

However, using a NoSQL Db, this URI could mean that we are accessing Zoo Document 12. This document would then likely have a 'animals' array property, which then may iterpret the the animal as the 5th in the array.

The purpose of REST is to abstract the traditional approach of Relation Database and restrict users from requesting Resources that are out of scope or which they are not authorized to access.

I like to think of it as walking through a building. When you are in a given room, you can only access rooms that are adjacent to one and other and that have a door available to them. If there is no door (or window, staircase, ladder,etc) in the current room to move to a different resource, then you wouldn't be able to access those rooms without finding a different entry place and path.

Upvotes: 1

Remario
Remario

Reputation: 3863

Not necessarily , the concept of rest is route determinant and request determinant. To expound further the DELETE /zoos/12/animals/5 - Deletes animal #5 for zoo #12.This means that the request (delete) for the route /zoos/12 access the 12 zoo,probably stored in some database.then animals for that 12 zoo (/zoos/12/animals/5) this case were getting any animal with id 5(the /zoos/12/ must exist) . This pattern is referred to as absolute route addressing because the entire must be prepended. However there is relative route addressing where part of the route address could stored relative to current path.

If by any chance you are using absolute addressing, and do not include the entire url, then that route is not valid.

Upvotes: 0

Shane van Wyk
Shane van Wyk

Reputation: 1900

It all depends on the architecture and context.

In this case one would assume that for a Zoo each Animal has a unique Id.

So Zoo # 13 Animal #5 might be a different Animal than Zoo #12 Animal # 5

So you would not want to Delete Animal #5 because it might delete from all Zoos if you don't specify the Zoo Id

Upvotes: 1

Related Questions