Rest API Python Guide
Rest API Python Guide
There are whole books written about this topic, but I can
give you a quick start here. In HTTP, we have different
"methods", as they are called. GET and POST are the most
common; these are used by web browsers to load a page
and submit a form, respectively. In REST, you use these to
to indicate different actions. GET is generally used to get
information about some object or record that already exists.
2
What kind of data? These days, it's very common to pass
JSON objects. So the API may state that a POST to /tasks/
must include a single object with two fields, "summary" and
"description", like this:
{
"summary": "Get milk",
"description": "A half gallon of organic 2% milk."
}
The other thing you get back is the response body. When
your web browser GETs a web page, the HTML sent back is
3
the response body. For an API, this can response body can
be empty, or not - it depends on the API and the end point.
4
HTTP Methods for REST APIs
The HTTP verbs comprise a major portion of our “uniform
interface” constraint and provide us the action counterpart
to the noun-based resource. The primary or most-common-
ly-used HTTP verbs (or methods, as they are properly
called) are POST, GET, PUT, PATCH, and DELETE.
5
the new resource with the parent, assigning an ID (new re-
source URI), etc.
Examples:
POST http://www.example.com/customers
POST http://www.example.com/customers/12345/orders
6
often returns a 404 (NOT FOUND) or 400 (BAD RE-
QUEST).
Examples:
GET http://www.example.com/customers/12345
GET http://www.example.com/customers/12345/orders
GET http://www.example.com/buckets/sample
7
PUT → Update/Replace Object
PUT is most-often utilized for update capabilities, PUT-
ing to a known resource URI with the request body contain-
ing the newly-updated representation of the original re-
source.
8
bandwidth. It is not necessary to return a link via a Loca-
tion header in the creation case since the client already set
the resource ID.
Examples:
PUT http://www.example.com/customers/125
PUT http://www.example.com/customers/125/orders/98
PUT http://www.example.com/buckets/secret_stuff
9
PATCH → Updated/Modify Object
PATCH is used for modify capabilities. The PATCH re-
quest only needs to contain the changes to the resource, not
the complete resource.
10
client can use a strong ETag in an If-Match header on the
PATCH request.
Examples:
PATCH http://www.example.com/customers/125
PATCH http://www.example.com/customers/125/orders/98
PATCH http://www.example.com/buckets/secret_stuff
11
DELETE on that resource ends up the same: the resource is
gone. If calling DELETE say, decrements a counter (within
the resource), the DELETE call is no longer idempotent. As
mentioned previously, usage statistics and measurements
may be updated while still considering the service idempo-
tent as long as no resource data is changed. Using POST for
non-idempotent resource requests is recommended.
Examples:
DELETE http://www.example.com/customers/12345
DELETE http://www.example.com/customers/12345/orders
DELETE http://www.example.com/bucket/sample
12
This section is based on tfredrich's excellent REST API tu-
torial available under the Creative Commons Attribution-
ShareAlike 4.0 International License.
13