URI template for POST / PUT service

I am going to write a restful API, my requirement is to call methods in the Transaction object, I was wondering how I should call Post / PUT with the corresponding URI template so that I can create / update the transaction resource without using "verbs" in the Uri display.

[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 note that I want to apply the best practice for matching uri and do not want it to have โ€œverbsโ€, but only โ€œnounsโ€. Also tell how the client will gain access to these methods for Post and Put with a unique URI. Thanks

+8
rest api wcf
source share
3 answers

You need to map the URI as shown below for Transaction .

Get transaction by ID - GET - transaction / id

Create a new transaction - POST - transaction

Update transaction - PUT - transaction / id

Delete transaction - DELETE - transaction / id

URI templates should be changed 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 the client will access these methods for Post and Put with a unique URI

You do not need a unique URI for POST and PUT. There URIs may be the same.

Links: 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

+13
source share

PUT is designed to create or update a known resource, for example: PUT / Transactions / 1234

This will create (or update, if it already exists) a transaction with an identifier of 1234. This means that you can use PUT only if you know the URL of the resource.

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

This will create a new transaction resource.

Note that I am pluralizing a transaction, so now it represents a collection.

Not being a C # developer, I donโ€™t know how easily this maps to WCF, but this approach is technology independent.

+2
source share

To create the correct URLs and api design principles ... I found this book (not mine!), Should read: http://offers.apigee.com/api-design-ebook-rr/

-one
source share

All Articles