views:

958

answers:

2

How would one create a REST web service to write a row into a databse table. Use the follwoing scenario:

The table is called Customer - the data to be inserted into the row would be the name, addresss, telephone number, email.

I think its impossible to describe the whole thing end to end in Java or C#, and I would never expect that, but here are the questions I have popping into my head as I prepare for coding:

  • How the URI would look (eg. if you use this URL - http://www.example.com/)?
  • What info would go into the HTTP envelope?
  • Would I use POST when writing to the database in this way?
  • Do I use a resource to store the posted data from the client? Is this even necessary if the data is being written to a database anyway?
  • When the data to be writeen into the db is recieved by the server - how do I physically insert it into the database - do I call some method on the server to actually write the data (in Java)? - this doesn't seem to fit with truely REST architecture - shunning RPC calls.
  • Should I even be bothering writing to a DB - should I be storing my data as a resource?

As you can see I need a few issues clearing in my head. Any help much appreciated.

+1  A: 
George Stocker
I've got this book now - (well comuple of months ago actually) - and it's pretty essential reading if a bit of heavy read. Looking at my original quesion - I really had no clue about RESTful web service - amazed I actually articulated a semi coherant question!
Vidar
+3  A: 

First of all, I'm not either java nor c# expert and I don't exactly know what means do these languages have to support REST design, but in general:

  1. http://www.example.com/customers - customers is a collection of resources and you want to add a new resource to this collection

  2. It depends on various things - you should probably set the content-type header (according to the data format in which you are sending the representation) and set some authentication headers if you need it.

  3. Yes, you always use POST to create a new entry in a collection of resources.

  4. I don't fully understand this question, to be honest. What do you mean by "inmediately writing data into the database"?

  5. REST is primarily just a style of communication between server and a client. It doesn't say anything about how you should handle the data received by using it. The usual way how modern web approaches (MVC style frameworks) solve it, is by routing every REST action to a method of some class (usually a controller instance) where you handle the received parameters (eg. save them to the database) and generate a response to be sent back.

For a very brief and very clear introduction to REST have a look at this short video.

Milan Novota
+1 I was half way through writing something very similar but you type faster :)
Paul
Happens to me sometimes. Hope you weren't too far with writing. I'm sorry :)
Milan Novota