Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Advanced HTTP methods explained

Sunday, December 11th, 2016 by Servage

http-methodsGET and POST requests are the most common HTTP methods, but these are not the only ones you can use in your applications. You may have heard about PUT, PATCH and HEAD, and if you are working on an RESTful API, you should be using them. Let’s see what these less common HTTP methods are and when you should use them.

PUT Request

When speaking from a RESTful viewpoint, a POST request is used to create a new resource. When a resource has already been created, it can be updated with a PUT request.

PUT replaces the entire resource, so you you are essentially recreating it when using a PUT request. This means you have to send a full representation of the resources, or in other words, all fields of the resource, when using PUT. If the target resource does not exist, it will be created. As an example, PUT http://example.com/customers/1/orders/1 would update all fields of the order and return the status code 200 (OK) if the update was successful.

To sum this up, PUT can be to thought as “replace”.

PATCH Request

Instead of updating the whole resource at once, you can only update some specific fields with a PATCH request. You can specify these fields by using the name attribute in the form you are using to make the PATCH request.

PATCH is identical to PUT with the exception that only the fields you send will be updated. All other fields will keep their old values. An example of this could be: PATCH http://example.com/users/1/products/1. In this case, you should have the fields you want to modify in your form.

DELETE Request

DELETE is the easiest method: it simply deletes the entire resource. For example to delete a user with the ID of 1, you can send a DELETE request to http://example.com/users/1. A successful request returns the status code 200. Notice that DELETE always removes the whole resource, not a single field.

HEAD Request

Like PATCH is related to PUT, HEAD is related to GET. The difference between HEAD and GET is that HEAD only returns the headers of the response. The body, which contains the actual content, is not sent.

OPTIONS Request

This is a method you don’t see very often, but is an important one for sure. OPTIONS can be used to query a specific URI to check what methods it supports. If you are unsure what methods have been implemented for some URI, send an OPTIONS request first and then a supported type of request that best suits your needs.

Advanced HTTP methods explained, 5.0 out of 5 based on 2 ratings
Categories: Tips & Tricks

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.