REST API - What should my post return?

Here is a short description of what I am trying to do

  • Build a set of APIs for an online game.
  • my GET API I want to return all open games (list of games) GET / api / games /
  • my POST API I want to create a new game POST / api / games /

My question is how to get my POST API back. Ideally I would like the client to know the gameId of the newly created game, so that all game specific operations can then use the following syntax / api / games / gameId

Any suggestions how can I transfer this game back to the client?


source to share

1 answer

You should return 201 generated as response code. The location header must contain the URL of the newly created resource (eg. / Api / games / new_game_id).

More information can be found in the answers to this question , in particular the link to the RFC.



All Articles