Image alternative text

Name: Factory

A factory resource is one that is used to create other resources.

Problem

Sometimes it is difficult to create a complete resource representation on the client without some involvement from the server. It may be that the server is responsible for creating a unique identifier for the new resource, so a PUT request is not possible. Or other attributes of the resource may be dependent on existing server state. For example, creating a order resource where the order quantity may be adjusted based on in-stock quantity.

Solution

The POST method is used to indicate an unsafe request is being made and the response should return a 201 - Created with a Location header that points to the newly created resource.

x
1
POST /FooFactory
2
3
=>
4
5
201 Created
6
Location: http://example.org/foo/99

There may or may not be a request body. For some resource factories it may be required to pass some kind of parameters in the request body. In other cases a request body may just be used to override default values.

The response body may choose to include a representation of the newly created resource, in which case a Content-Location header may be appropriate. Or the response body could contain status information related to the creation request.

It may be useful for the ResourceFactory to implement a GET method that can return a template with default values that can be modified by the client before being POSTed back to the ResourceFactory.

Discussion

This pattern naturally occurs for most scenarios where POST is used to create new resources. Often the factory resource doubles as a collection of the resource to be created. e.g. POST /messages might create a resource that appears structurally as a child resource /messages/75. However, there is no requirement for the new resource to be created with a URL that has any correlation to the ResourceFactory.

Related Patterns

[Whatif]