Create single and multiple resources using restful HTTP

前端 未结 5 927
时光取名叫无心
时光取名叫无心 2021-02-07 00:21

In my API server I have this route defined:

POST /categories

To create one category you do:

POST /categories {\"name\": \"Books         


        
5条回答
  •  北海茫月
    2021-02-07 00:44

    There's nothing particularly wrong with having a bulk operation that you POST to, to activate (it'll be non-idempotent so POST is the right verb) but there are some caveats:

    • You're making multiple resources, so you need to respond with multiple URLs. This means you can't use the redirect pattern: you'll have to send a list of URLs back in some form.

    • You have a problem in that bulk operations are often not very discoverable. Discoverability is one of the most important things about RESTfulness, as it means that someone can come along and figure out how to write a client without lots of help from the server author.

    • Dealing with partial failures when you've got bulk operations remains problematic. It's a problem with any other paradigm too (I've watched people tie themselves in knots over this when working with extensions to SOAP) so it isn't a surprise, but unless you can guarantee that all the creations will work, you're going to have to work out what happens when you make one resource and fail to make the second. (Also, if the bulk request wanted a third one done, would you go on and try that?)

    The simplest approach is just to support one create per request; that's a much easier pattern to get right and is better understood all round.

提交回复
热议问题