0

I am working on a Ticket Reservation application. I am required to build a web-service which takes in request for cancellation, checks with a downstream system (via SOAP), whether it can be cancelled, and returns the appropriate response to the calling system after updating my tables.

I have to build a bunch of similar web-services for cancellation, rescheduling, et for which I'm planning to use RESTful web-services

This I'm planning to achieve using the POST method of the REST API, using JAX-RS. I could see that all the web-services I'm going to build suit the POST verb, and the other HTTP methods (GET, POST and DELETE) doesn't seem like they need to be used here. In that case what is the actual use case of these HTTP methods in REST? Am I right in using only POST?

2
  • You don't need to .... that is the beauty of it.. your are not tied to make endpoints for them. Commented Jan 13, 2014 at 15:16
  • Ditto gtgaxiola. There is a great deal of consensus as to what each HTTP verb should do in REST. PUTs result in reciprocal GETs. POSTS modify. That's the very short version. ;) Unless a Cancellation was an entity in your system that could be created and retrieved, PUT and GET have no business being implemented. Commented Jan 13, 2014 at 15:19

4 Answers 4

3

Your REST resource is the ticket. Use GETs to read the ticket, PUT to modify the ticket state, and DELETE to do a logical delete (cancellation).

For any other operation, use the wildcard operation POST.

Have a look at this blog post Brief Introduction to REST, where the author goes through the core REST principles.

Sign up to request clarification or add additional context in comments.

8 Comments

the thing I'm failing to understand here, are these treated differently GET/PUT/DELETE/POST? Or is just good architecture? And just to make sure u adhere to the REST architecture?
It's just to follow the normal way these methods are used, in their usual meaning as per the HTTP spec: GET reads with no side effects on the server, DELETE does logical delete, PUT any other changes of state and can called many times with the same result, POST is for anything else. This is just convention for a good REST architecture, using the methods for what they where meant in the HTTP spec. Nothing prevents for example from using only GET, and passing parameters GET /tickets/myTicketId?delete=true and doing deletes like that, but it would be surprising for users of the API
Also some bot could come along and start deleting stuff, etc. It's all convention but eveything tends to work better if we adere to it
Does the browser treat the requests differently?
It does not make any difference in terms of processing the request, It's is more correct to use DELETE because that is it's conventional meaning of logical delete (cancellation). It is a more correct way of following the HTTP conventions, but that does not mean that doing it over POST would not work
|
2

For the purpose of a cancellation, I would actually use the DELETE method. In the case of a successful cancellation, return a 200 with either response body confirming success or a response body containing the details of the reservation. Or a 204 if you don't want to return a body.

If it can't be canceled, you have your choice from the 400 series of errors (e.g. 404 if there is no such reservation, 403 if the request is forbidden, etc.) or the 500 series if there is a bug or malfunction.

You will certainly use the other HTTP verbs for all the other reservation actions--making a reservation, changing it, etc.

2 Comments

My cancellation all it does at the end is change the status from Booked to Cancelled. Does this require a DELETE method?
This is where things get fuzzy with the interpretation of REST, but here is my interpretation. I wouldn't say cancellation "requires" a DELETE, but cancellation strikes me as what a DELETE represents in the eyes of the caller. The fact your implementation is basically just setting a flag is an implementation detail irrelevant to the caller. All he knows is he is deleting a resource (the reservation). But if a user can see all reservations--canceled or otherwise--in an account history or similar, then maybe POST would make more sense.
0

Consider your Ticket Reservations as your application resources. Clients to your application would:

  • submit GETrequest to retrieve a representation (XML, JSON or anything else) one or many ticket reservations (given the URL they use, eg: /tickets to retrieve all of them - or all they are allowed to see, /tickets/1to retrieve a representation of the Ticket Reservation whose id is 1, tickets/?start=0&size=10 for paginated content, etc.)
  • submit POST requests on /ticketsto create a new reservation (and the response would provide the client with a location giving the location of the created reservation)
  • submit a PUT request to /tickets/1 (with a representation of the new reservation in the request body) to update the reservation identified by 1
  • submit a DELETE request to /tickets/1 to delete the reservation identified by 1

The main idea behind the use of such verbs is that the URLs should identify resources (and not actions), and the HTTP verb would be the action.

If you use everything with POST, you'll be more or less doing SOAP.

This article will give you more information on hat I tried to hihlight above: http://martinfowler.com/articles/richardsonMaturityModel.html

Comments

0

There's quite a lot to say about designing RESTful applications. You really should read one or more of these books: "Restful Web Services" - http://amzn.com/0596529260, "RESTful Web APIs" - http://amzn.com/B00F5BS966, "REST in Practice: Hypermedia and Systems Architecture" - http://amzn.com/B0046RERXY or "RESTful Web Services Cookbook" - http://amzn.com/B0043D2ESQ.

Get a cup of coffee and enjoy some reading :-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.