Skip to the content.

HTTP

Different API mindset:

Features

URL

REST URL can be:

GraphQL URL can be:

HTTP persistent connection

CRUD methods (HTTP requests)

Method Resource Parameters Meaning Usual response
POST /books Request body Create a book 201
GET /books Query string Retrieve all books 200
GET /books/{id} {id} + Query string Read one book 200
PUT /books/{id} {id} + Body Update a book 200 or 204
DELETE /books/{id} Path {id} Delete a book 204
DELETE /books   Delete all books 204

The PUT and DELETE methods may also accept query string parameters. These query string parameters are often common with the ones of the GET method for consistency reasons.

The GET /books may accept optional parameters (query string) to provide a response with pagination, sorting, ordering and filtering.

The DELETE is also used in the disable or cancel meanings, not always in the erase meaning. Some API may implement the DELETE /books method using request body parameters to filter multiple resources to mass delete/disable/cancel.

The request body usually conveys the parameters in the JSON format, but other formats can be used such as plain text, XML, CSV or even binary format in base64 representation.

The API may also accept other HTTP request methods:

HTTP response status codes

Inspired from:

Status Classes

Create POST /books

To create a new resource or to access a token.

Read GET /books/{id}

To retrieve a resource representation.

Read GET /books

To fetch multiple resource representations.

Update PUT /books/{id}

To update a resource.

Delete DELETE /books/{id}

To delete (or disable/cancel) a resource.

Delete DELETE /books

To delete, disable or cancel all resources.

API versioning

The URL contains the major version number {v1} such as:

https://api.example.com/{v1}/books

To allow a client to test the API in a simulation environment, the URL can be:

https://api-sandbox.example.com/{v1}/books

To allow a client to test a minor API change (e.g. a new feature backward compatible), the URL can be:

https://api-sandbox-next.example.com/{v1}/books

or:

https://api-next.example.com/{v1}/books`

Developers may use:

https://api-sandbox-dev.example.com/{v1}/books

When an API lives long enough, sooner or later, its structure will change. To avoid breaking changes, the new API version should status codes of the redirect class. This will help, especially for the clients automatically following the Location header.

Multiple endpoints for one resource

The API using nested or sub-resources can only deliver representations from root (non-nested) resources. For example, the two endpoints /books/{id}/comments/1234 and /users/{u}/comments/1234 represent the same resource (a user’s comment of a book) that can also be access using /comments/1234.

Permission errors

Other errors

See also

https://gist.github.com/subfuzion/669dfae1d1a27de83e69

WebSocket

To be completed…