Antoine Martin
Antoine Martin

Reputation: 1953

RESTful - add subresource to a resource : POST /subresource or POST /resource/id/subresource?

I want to add an attachment to a case.

Which request should I choose?

POST /cases/123/attachments
{
   "fileName" : "subscription.pdf",
   "content" : "JVBERi0xLjYNJeLjz9MNCjE1MzMgMCBvYmoNPDwvTGluZWF..."
}

or

POST /attachments
{
   "caseId" : 123,
   "fileName" : "subscription.pdf",
   "content" : "JVBERi0xLjYNJeLjz9MNCjE1MzMgMCBvYmoNPDwvTGluZWF..."
}

I guess both are correct.

Upvotes: 2

Views: 1045

Answers (3)

Jonas Tomanga
Jonas Tomanga

Reputation: 1108

If a single attachment can be shared amongst resources then the second one is correct. However, if an attachment is not independent of a case then the 1st one makes much better sense.

Can an attachment make sense without its case?

Upvotes: 1

inf3rno
inf3rno

Reputation: 26147

Both are correct. I would choose the second one, I don't like hierarchical URIs, since they get complicated by describing multi level hierarchies. I would use GET /attachments/?case=id to retrieve the attachments of a case.

Upvotes: 0

laurent
laurent

Reputation: 90863

Both are fine though I'd go for the first one, since it makes the relation between cases and attachments more explicit. And then you can also retrieve all the attachments for a cases using GET /cases/:id/attachments

Upvotes: 4

Related Questions