1

basically, I want to POST a resource C to the server. Depending on cases, the creation of a resource C may create resources A and B too. As relationship matters, C is nested in B, himself nested in A.

In that case, we need to POST more data than just the resource C, hence, we'll be POSTing a write only resource, D, that doesn't have any meaning expect in this case.

My question is : What shall my api send back ? 200 Okay, 201 Created ? Shall I send the created resource C ?

Thanks.

Paul L
  • 97
  • 9
  • 1
    Giving a real life example could make it easier to understand the question. – Jan Zyka Aug 04 '14 at 13:53
  • I wish this question to stay as theoretical as possible, thus avoiding real example. If I am unclear, I'll do my best to answer your questions. – Paul L Aug 04 '14 at 13:57

1 Answers1

0

It should return 201 whenever you want to tell user the action succeeded and as a result an entity was created.

The newly created recrod should be included as well.

I'm writing this based mostly on the Rest API tutorial.

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • The thing is : I'm asked to create D, and I answer with C, isn't it a bit strange ? – Paul L Aug 04 '14 at 14:56
  • I find strange that you have one URL and based on content you create 2 different entities. I would say you should have two different request. One for creating C only (which fails if the parent doesn't exist) and one for creating D. It sounds like you know on the client which is the case (you are sending different parameters). So I would split it. It's hard to tell though when there is no real life example. – Jan Zyka Aug 04 '14 at 15:28
  • This all sounds like url "/createCorDTogetherWithCBasedOnWhatIsInTheRequest". In my understanding you should have split url for creating the separate resources – Jan Zyka Aug 04 '14 at 15:32
  • D is more like C with more infos to either find which B this C should refer to or create A, B and C. – Paul L Aug 04 '14 at 16:02
  • It's hard to tell more, but in such case I would expect separate requests. To create C under particular B it would be like POST /C/{idOfC}/B, if you need to create C first do 2 steps: POST /C and then based in what C returns POST /C/{idOfC}/B etc. – Jan Zyka Aug 05 '14 at 06:25
  • Oh, isn't this what you are really concerned about: http://stackoverflow.com/questions/147207/transactions-in-rest – Jan Zyka Aug 05 '14 at 06:27
  • On the API caller side, I don't know B id. On server side, an algorithm will run to find if this C relates to an already existing B or not. – Paul L Aug 05 '14 at 08:10
  • Since the difference between D and C is kept in A and B for storages matter, what about POSTing C nested in a (very) partial B and a (quite much) partial A ? Hence I would make a POST on A. Would it be REST ? – Paul L Aug 05 '14 at 08:11