-1

In our application we have a user who can be assigned multiple territories. What is the best way to expose the REST API?

Should it be as :

[POST] www.xyz.com/Territory/User

OR

[POST] www.xyz.com/User/Territory

BODY :

{ "UserId" : 6, "TerritoryId" : [1,2,5] }

I am designing this application using .net WEB API 2, so should the above action be placed in User controller or Territory controller or shall I create UserTerritory as a new controller? Any help is highly appreciated.

2 Answers 2

1

REST doesn't care what spelling you use for your identifiers.

 GET /715f9784-4b51-461a-b3ee-fdf2823c25cb

is perfectly fine, as far as REST is concerned.

 www.xyz.com/Territory/User
 www.xyz.com/User/Territory

The only real difference between these two, seen from the client, is the way that relative references work. Where are dot-segments going to be useful?

www.xyz.com/Territory/User + ../Foo = www.xyz.com/Territory/Foo
www.xyz.com/User/Territory + ../Foo = www.xyz.com/User/Foo

If you are more interested in other things about Territory, then putting it at the root makes it easier to reach other resources starting with that stem; if you are more interested in other resources about User, then that belongs up top.

The good news: HTTP has standard semantics in place if you get it "wrong"

GET /Territory/User/...

307 Temporary Redirect
Location: /User/Territory/...
-2

Because you are wisely using POST you don't have any parameters in the URL

Thus, you can use either or both or simply

www.xyz.com/User/AddUser
{ "UserId" : 6, "TerritoryId" : [1,2,5] }

If you are GETing users you would expect

/Territory/{territoryId}/Users

To return only the users in the selected territory

Whereas the Users controller would be used for more generic queries, possibly requiring a POST

POST /User/GetUserByTerritoryIds  
{[1,2,3,4....]}
2
  • 1
    I like to avoid verbs when a resource name can be used in this case "Add" on AddUser could be replaced with /Territory/{terriority}/Users and a POST that adds a user to a given territory. Commented Jun 20, 2018 at 15:17
  • It sure makes the examples clearer though doesnt it Commented Jun 20, 2018 at 15:24

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.