|
|
|
@ -5,6 +5,7 @@ weight: 30
|
|
|
|
---
|
|
|
|
---
|
|
|
|
The HTTP verbs (or methods, as they are formally called) comprise a major portion of our “uniform interface” constraint and provide us the action counterpart to the noun-based resource. The primary or most-commonly-used HTTP verbs are POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively. There are a number of other verbs, too, but are utilized less frequently. Of those less-frequent methods, OPTIONS and HEAD are used more often than others.
|
|
|
|
The HTTP verbs (or methods, as they are formally called) comprise a major portion of our “uniform interface” constraint and provide us the action counterpart to the noun-based resource. The primary or most-commonly-used HTTP verbs are POST, GET, PUT, PATCH, and DELETE. These correspond to create, read, update, and delete (or CRUD) operations, respectively. There are a number of other verbs, too, but are utilized less frequently. Of those less-frequent methods, OPTIONS and HEAD are used more often than others.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Overview
|
|
|
|
Below is a table summarizing recommended return values of the primary HTTP methods in combination with the resource URIs:
|
|
|
|
Below is a table summarizing recommended return values of the primary HTTP methods in combination with the resource URIs:
|
|
|
|
|
|
|
|
|
|
|
|
| Method | CRUD | Entire Collection (e.g. /customers) | Specific Item (e.g. /customers/{id}) |
|
|
|
|
| Method | CRUD | Entire Collection (e.g. /customers) | Specific Item (e.g. /customers/{id}) |
|
|
|
|
@ -16,7 +17,7 @@ Below is a table summarizing recommended return values of the primary HTTP metho
|
|
|
|
| DELETE | Delete | 405 (Method Not Allowed), unless you want to delete the whole collection—not often desirable. | 200 (OK). 404 (Not Found), if ID not found or invalid. |
|
|
|
|
| DELETE | Delete | 405 (Method Not Allowed), unless you want to delete the whole collection—not often desirable. | 200 (OK). 404 (Not Found), if ID not found or invalid. |
|
|
|
|
|
|
|
|
|
|
|
|
Below is a more-detailed discussion of the main HTTP methods.
|
|
|
|
Below is a more-detailed discussion of the main HTTP methods.
|
|
|
|
### POST
|
|
|
|
## POST
|
|
|
|
The POST verb is most-often utilized to *create* new a resource. In particular, it's used to create subordinate resources. That is, subordinate to some other (e.g. parent) resource. In other words, when creating a new resource, POST to the parent and the service takes care of associating the new resource with the parent, assigning an ID (new resource URI), etc.
|
|
|
|
The POST verb is most-often utilized to *create* new a resource. In particular, it's used to create subordinate resources. That is, subordinate to some other (e.g. parent) resource. In other words, when creating a new resource, POST to the parent and the service takes care of associating the new resource with the parent, assigning an ID (new resource URI), etc.
|
|
|
|
|
|
|
|
|
|
|
|
On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the 201 HTTP status.
|
|
|
|
On successful creation, return HTTP status 201, returning a Location header with a link to the newly-created resource with the 201 HTTP status.
|
|
|
|
@ -27,7 +28,7 @@ __Examples:__
|
|
|
|
* POST http://www.example.com/customers
|
|
|
|
* POST http://www.example.com/customers
|
|
|
|
* POST http://www.example.com/customers/12345/orders
|
|
|
|
* POST http://www.example.com/customers/12345/orders
|
|
|
|
|
|
|
|
|
|
|
|
### GET
|
|
|
|
## GET
|
|
|
|
The HTTP GET method is used to **read** (or retrieve) a representation of a resource. In the “happy” (or non-error) path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).
|
|
|
|
The HTTP GET method is used to **read** (or retrieve) a representation of a resource. In the “happy” (or non-error) path, GET returns a representation in XML or JSON and an HTTP response code of 200 (OK). In an error case, it most often returns a 404 (NOT FOUND) or 400 (BAD REQUEST).
|
|
|
|
|
|
|
|
|
|
|
|
According to the design of the HTTP specification, GET (along with HEAD) requests are used only to read data and not change it. Therefore, when used this way, they are considered safe. That is, they can be called without risk of data modification or corruption—calling it once has the same effect as calling it 10 times, or none at all. Additionally, GET (and HEAD) is idempotent, which means that making multiple identical requests ends up having the same result as a single request.
|
|
|
|
According to the design of the HTTP specification, GET (along with HEAD) requests are used only to read data and not change it. Therefore, when used this way, they are considered safe. That is, they can be called without risk of data modification or corruption—calling it once has the same effect as calling it 10 times, or none at all. Additionally, GET (and HEAD) is idempotent, which means that making multiple identical requests ends up having the same result as a single request.
|
|
|
|
@ -39,7 +40,7 @@ __Examples:__
|
|
|
|
* GET http://www.example.com/customers/12345/orders
|
|
|
|
* GET http://www.example.com/customers/12345/orders
|
|
|
|
* GET http://www.example.com/buckets/sample
|
|
|
|
* GET http://www.example.com/buckets/sample
|
|
|
|
|
|
|
|
|
|
|
|
### PUT
|
|
|
|
## PUT
|
|
|
|
PUT is most-often utilized for *update* capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.
|
|
|
|
PUT is most-often utilized for *update* capabilities, PUT-ing to a known resource URI with the request body containing the newly-updated representation of the original resource.
|
|
|
|
|
|
|
|
|
|
|
|
However, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server. In other words, if the PUT is to a URI that contains the value of a non-existent resource ID. Again, the request body contains a resource representation. Many feel this is convoluted and confusing. Consequently, this method of creation should be used sparingly, if at all.
|
|
|
|
However, PUT can also be used to create a resource in the case where the resource ID is chosen by the client instead of by the server. In other words, if the PUT is to a URI that contains the value of a non-existent resource ID. Again, the request body contains a resource representation. Many feel this is convoluted and confusing. Consequently, this method of creation should be used sparingly, if at all.
|
|
|
|
@ -57,7 +58,7 @@ __Examples:__
|
|
|
|
* PUT http://www.example.com/customers/12345/orders/98765
|
|
|
|
* PUT http://www.example.com/customers/12345/orders/98765
|
|
|
|
* PUT http://www.example.com/buckets/secret_stuff
|
|
|
|
* PUT http://www.example.com/buckets/secret_stuff
|
|
|
|
|
|
|
|
|
|
|
|
### PATCH
|
|
|
|
## PATCH
|
|
|
|
PATCH is used for *modify* capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource.
|
|
|
|
PATCH is used for *modify* capabilities. The PATCH request only needs to contain the changes to the resource, not the complete resource.
|
|
|
|
|
|
|
|
|
|
|
|
This resembles PUT, but the body contains a set of instructions describing how a resource currently residing on the server should be modified to produce a new version. This means that the PATCH body should not just be a modified part of the resource, but in some kind of patch language like JSON Patch or XML Patch.
|
|
|
|
This resembles PUT, but the body contains a set of instructions describing how a resource currently residing on the server should be modified to produce a new version. This means that the PATCH body should not just be a modified part of the resource, but in some kind of patch language like JSON Patch or XML Patch.
|
|
|
|
@ -69,7 +70,7 @@ __Examples:__
|
|
|
|
* PATCH http://www.example.com/customers/12345/orders/98765
|
|
|
|
* PATCH http://www.example.com/customers/12345/orders/98765
|
|
|
|
* PATCH http://www.example.com/buckets/secret_stuff
|
|
|
|
* PATCH http://www.example.com/buckets/secret_stuff
|
|
|
|
|
|
|
|
|
|
|
|
### DELETE
|
|
|
|
## DELETE
|
|
|
|
DELETE is pretty easy to understand. It is used to *delete* a resource identified by a URI.
|
|
|
|
DELETE is pretty easy to understand. It is used to *delete* a resource identified by a URI.
|
|
|
|
|
|
|
|
|
|
|
|
On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.
|
|
|
|
On successful deletion, return HTTP status 200 (OK) along with a response body, perhaps the representation of the deleted item (often demands too much bandwidth), or a wrapped response (see Return Values below). Either that or return HTTP status 204 (NO CONTENT) with no response body. In other words, a 204 status with no body, or the JSEND-style response and HTTP status 200 are the recommended responses.
|
|
|
|
|