Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create single and multiple resources using restful HTTP

In my API server I have this route defined:

POST /categories 

To create one category you do:

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

I thought that if you want to create multiple categories, then you could do:

POST /categories [{"name": "Books"}, {"name": "Games"}] 

I just wanna confirm that this is a good practice for Restful HTTP API.

Or should one have a

POST /bulk 

for allowing them to do whatever operations at once (Creating, Reading, Updating and Deleting)?

like image 321
wingy Avatar asked Jun 20 '12 14:06

wingy


People also ask

How do I create a resource in REST API?

Resources are typically created by sending a POST request to the parent collection resource. This creates a new subordinate resources with a newly generated id. For example, a POST request to /projects might be used to create a new project resource at /projects/123.

Does http create resources?

The HTTP PUT request method creates a new resource or replaces a representation of the target resource with the request payload. The PUT method requests that the enclosed entity be stored under the supplied Request-URI.

What are RESTful resources?

What is a Resource? REST architecture treats every content as a resource. These resources can be Text Files, Html Pages, Images, Videos or Dynamic Business Data. REST Server simply provides access to resources and REST client accesses and modifies the resources. Here each resource is identified by URIs/ Global IDs.

Can a resource have multiple URLs?

Note: So, the point that you need to remember is, each resource must have a unique URL, and also it is possible that a resource can be accessed using multiple URLs as long as all the URLs are unique. But it is not possible to access two or more different resources using a single URL in ASP.NET Core Web API Application.


1 Answers

In true REST, you should probably POST this in multiple separate calls. The reason is that each one will result in a new representation. How would you expect to get that back otherwise.

Each post should return the resultant resource location:

POST -> New Resource Location POST -> New Resource Location ... 

However, if you need a bulk, then create a bulk. Be dogmatic where possible, but if not, pragmatism gets the job done. If you get too hung up on dogmatism, then you never get anything done.

Here is a similar question

Here is one that suggests HTTP Pipelining to make this more efficient

like image 53
Justin Pihony Avatar answered Oct 08 '22 20:10

Justin Pihony