Explain and example about 'get', 'delete', 'post', 'put', 'options', 'patch', 'head' method?

Qui Nguyen picture Qui Nguyen · Nov 20, 2014 · Viewed 20.8k times · Source

I'm writing a webservice. Could any one explain these above methods and give me some example about them? Thank for your help.

Answer

Amrit picture Amrit · Dec 15, 2017

GET should be used to retrieve data with no other effect however you can use query params in url to post data using get but it is not a safe method.

The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.Generally used to create new entity.

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. Generally used to update existing entity.

The PATCH method applies partial modifications to a resource

The DELETE method requests that the origin server delete the resource identified by the Request-URI.

The TRACE method echoes the received request so that a client can see what (if any) changes or additions have been made by intermediate servers.

The HTTP CONNECT method method starts two-way communications with the requested resource. It can be used to open a tunnel usually to facilitate SSL-encrypted communication (HTTPS) through an unencrypted HTTP proxy.

The OPTIONS method allows the client to determine the options and/or requirements associated with a resource, or the capabilities of a server, without implying a resource action or initiating a resource retrieval.

HEAD Retrieve all resources in a collection (header only) i.e. The HEAD method asks for a response identical to that of a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.

From this_link you can get a detail about these methods.I have used these resources to write these methods in short.

You can also get simplified details on this wikipidea page. This stackoverflow link is also very descriptive for http methods.

And for the implementation part this open source Django_rest_code at github can be a very good example to look at how to implement these Http methods in Django(Python).