Reputation: 16864
I have a back-end service that creates unique identifiers for resources.
The general idea is that resources are saved and versioned, so you can perform:
GET http://service/sales/targets/7818181919/latest
or
GET http://service/sales/targets/7818181919/4 for version 4
, and so on.
My question is about the most correct way to upload these resources in the first place.
How about:
PUT http://service/sales/targets/
returning 303 See other /service/sales/targets/
It seems a little wrong as you should PUT and GET from exactly the same place using a resource-oriented interface, but I can't think of a better option. Any ideas?
Upvotes: 1
Views: 129
Reputation: 26383
You should set the Location header when you POST or PUT the resource. The target for the POST should be the appropriate 'container' resource (http://service/sales/targets/ in your case)
See here for more details on the HTTP Headers. I've quoted the relevant section below. Hope that helps ...
14.30 Location
The Location response-header field is used to redirect the recipient to a location other than the Request-URI for completion of the request or identification of a new resource.
For 201 (Created) responses, the Location is that of the new resource which was created by the request. For 3xx responses, the location SHOULD indicate the server's preferred URI for automatic redirection to the resource.
The field value consists of a single absolute URI.Location = "Location" ":" absoluteURI
An example is:
Location: http://www.w3.org/pub/WWW/People.html
Chris
Upvotes: 1
Reputation: 21241
If you create a resource where you don't know yet the exact URI, then use POST
against the "collection resource" (in your case http://service/sales/targets) and return the new URI in the response Location
header. This can be compared to the factory pattern in OOP.
Alternatively you can provide a resource that generates unique identifiers. This allows your clients to GET
an unique identifier first and then use PUT
against an URI using the identifier. The downside is that you have to maintain a list of all unique identifiers that have been served so far, no matter if they have really been used or not.
Upvotes: 3