For example, by using HTTP GET request on /employee/101, you can retrieve details of that user.
By using POST on employe/102 would create a new user with employee id 102, PUT request type on /employee/101 can be used to update details of employee with id 101 and DELETE method on /employee/101 can be used to remove data for that id.
By the way, in the case of PUT and POST method representation would be in the request body. See RESTful Web Services by Leonard Richardson, Sam Ruby, and David Heinemeier Hansson for more details. One of the best book to learn fundamentals of RESTful WebService.
Purpose of HTTP Request in RESTful WebService
The purpose of each of the HTTP request types, when used with a RESTful web service, is as follows:GET
Retrieves data from the server (should only retrieve data and should have no other effect). This is also called an idempotent method. here is an example of a GET request to retrieve the book with id 123 from Server.
GET /books/123
POST
This HTTP request type is usually used for creating an entity i.e. a resource without an id. Once the request is successfully created, an id of the newly created entity is returned as part of the response to this HTTP request. It is often used when uploading a file or submitting a completed web form.
For example, following URL will create a new book in the server
POST /books/
PUT
Similar to POST, but used to update an existing entity. You pass the id of existing resource along with PUT request type. For example, following URL will replace the book with id 123 in the server
PUT /books/123
See RESTful Web Services by Leonard Richardson, Sam Ruby, and David Heinemeier Hansson for more details on when to use PUT and POST while creating RESTful API.
DELETE
Removes the resource from the server. Similar to PUT you need to pass the id of the resource to be deleted. For example, following URL will remove the book with id 123 in the server
DELETE /books/123
TRACE
Provides a means to test what a machine along the network path receives when a request is made. As such, it simply returns what was sent.
HEAD
Same as the GET method for a resource, but returns only the response headers (i.e., with no entity-body). Similar to the GET request, HEAD Is also idempotent i.e. no side effect on Server.
See REST in Practice: Hypermedia and Systems Architecture by Jim Webber, Savas Parastatidis, and Ian Robinson for more details on how to use different HTTP methods while designing RESTful API.
OPTIONS
Allows a client to request information about the request methods supported by a service. The relevant response header is Allow and it simply lists the supported methods. (It can also be used to request information about the request methods supported for the server where the service resides by using a * wildcard in the URI.)
CONNECT
Primarily used to establish a network connection to a resource (usually via some proxy that can be requested to forward an HTTP request as TCP and maintain the connection). Once established, the response sends a 200 status code and a “Connection Established” message.
rpose of different HTTP request types in RESTFul Web service, be it in Java or any other technology. Most important is to understand the difference between PUT and POST request types, though both PUT and POST can be used to create and update an entity, POST is usually preferred for creating and PUT is preferred for updating an existing entity.
No comments:
Post a Comment