0

I have a Resource named Company, that can have Post Resource,

I have a path for create of post as: POST /api/company/{companyName}/post.

My question is whether, must i use a path as PUT /api/company/{companyName}/post/{postId} or PUT /api/company/post/{postId} for Update of post resource?

  • I know that i have a bad and probably duplicate question, sorry I can not speak english very good. – ghanbari Nov 3 '15 at 10:06
  • 1
    Upto you, you can define custom routes. – JᴀʏMᴇᴇ Nov 3 '15 at 10:28
  • If you use something like hateos you could return a list of links and maybe the date and title at: /api/companies/{companyName}/posts which would link to: /api/posts/{postId}. The the list of posts for a company is a resource. And the post itself is another resource. – Luc Franken Nov 3 '15 at 13:38
3

For nested resources like this you will find both options.

/api/company/{companyName}/post/{postId}

would be the more detailed variant. While

/api/company/post/{postId}

is more concise (obviously it requires postId to be unique and the record to have a reference to the companyId).

For Ruby on Rails this type of routes is called shallow nesting and very often used and recommended. Though (at least for Rails) you would in such a case obviously have a third option and omit the company part completely:

/api/post/{postId}

unless you need this for anything else inside your code or have different types of posts that are handled differently.

There are not really any clear rules for that scenario (to my best knowledge). A URI only must be able to find a resource. You can even have multiple routes point to the same resource (just make sure you don't duplicate the code to handle them).

I would want to keep things simple, so at least decide for one variant and implement all necessary actions with that route. But it mostly depends on your use case and what makes most sense for the clients of your API.

So if you implement PUT and POST with different routes I would implement both methods on both variants (if there is a chance they will be used at all). So the client can decide if he wants to use /api/company/post/{post_id} or prefers /api/post/{post_id} but does not have to switch between both of them for no obvious reason.

  • thank you, yes postId is unique and post have a reference to company, i can change path to /api/post/{postId}, but i fear that this change break rest rules. have a long path for POST and a short path for PUT, is it matter? – ghanbari Nov 3 '15 at 10:27
  • @ghanbari updated my answer – thorsten müller Nov 3 '15 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.