RESTful way of getting a resource, but creating it if it doesn't exist yet

前端 未结 2 1326
南旧
南旧 2021-01-18 10:09

For a RESTful API that I\'m creating, I need to have some functionality that get\'s a resource, but if it doesn\'t exist, creates it and then returns it. I don\'t think this

2条回答
  •  青春惊慌失措
    2021-01-18 11:06

    very simple:

    1. Request: HEAD, examine response code: either 404 or 200. If you need the body, use GET.
    2. It not available, perform a PUT or POST, the server should respond with 204 and the Location header with the URL of the newly created resource.

提交回复
热议问题