URI template for POST/PUT restful service

I am going to write a restful API, my requirement is to call methods on “Transaction” object, I was wondering how I should call Post/PUT with appropriate URI template so that I can Create/update the Transaction resource without using “verbs” in Uri mapping.

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Transaction/{**What to write here ????**}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Transaction AddTransaction(Transaction transaction)
{
    return AddTransactionToRepository(transaction);
}

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/Transaction/{**What to write here ????**}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public Transaction UpdateTransaction(Transaction transaction)
{
    return UpdateTransactionInRepository(transaction);
}

Please consider that I want to apply best practice for uri mapping and do not want “verbs” in it, only “nouns”. Also tell me how client will access these methods for Post and Put with unique URI. Thanks

Answers


You have to map the URIs as below for Transaction.

Get a transaction by ID - GET - transaction/id

Create a new transaction - POST - transaction

Update a transaction - PUT - transaction/id

Delete a transaction - DELETE - transaction/id

Your URI templates has to be changed as below

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/Transaction", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public Transaction AddTransaction(Transaction transaction)
{
    //
}

[OperationContract]
[WebInvoke(Method = "PUT", UriTemplate = "/Transaction/{id}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
public Transaction UpdateTransaction(int id, Transaction transaction)
{
    //
}

how client will access these methods for Post and Put with unique URI

You don't need unique URI for POST and PUT. There URIs can be same.

References: http://www.asp.net/web-api/overview/creating-web-apis/creating-a-web-api-that-supports-crud-operations

http://msdn.microsoft.com/en-us/library/bb412172(v=vs.90).aspx


PUT is for creating or updating a known resource, for example: PUT /Transactions/1234

This would create (or update if it already exists) the transaction with the ID 1234. This means you can only use PUT when you know the URL of the resource.

POST creates a new child resource, for example: POST /Transactions/

This would create a new transaction resource.

Notice I pluralised Transaction so it now represents a collection.

Not being a C# developer, I don't know how easy this maps to WCF, but this approach is technology-independent.


For making proper urls and api design principles... I found this ebook (not mine!) a must read: http://offers.apigee.com/api-design-ebook-rr/


Need Your Help

Backbone.js progress bar while fetching collection

backbone.js progress-bar

i'd like to show a progress bar while i update the app with fresh content. I guess the best thing to do would be to do it while calling .fetch on a collection.

Modal won't close - Twitter Bootstrap 1.40

javascript twitter-bootstrap modal-dialog

I'm using Bootstrap 1.40 and whilst my modal opens without any problem, I can't seem to close it using the button I have included in the modal it self.