Skip to content

Commit dea4780

Browse files
committed
Cleaned up some of my notes on REST, since I was reading up on this, more might come
1 parent 38b689a commit dea4780

5 files changed

+80
-7
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -805,6 +805,8 @@
805805
- [Use proper headers](rest/use_proper_headers.md)
806806
- [Check out OpenAPI](rest/check_out_openapi.md)
807807
- [Use verbs](rest/use_verbs.md)
808+
- [Deprecating API Endpoints](rest/deprecating_api_endpoints.md)
809+
- [Notes on RESTful API design](rest/notes_on_restful_api_design.md)
808810

809811
<a id="rm"></a>
810812
### rm - remove files and directories on the command line

rest/deprecating_api_endpoints.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Deprecating API endpoints
2+
3+
As for deprecation indication using HTTP status code there is no clear specification.
4+
5+
Various answers on StackOverflow and the like tend towards: `410 Gone`.
6+
7+
Some suggest using: `301 Moved Permanently` or `302 Found`.
8+
9+
I do see some challenges with: `401` as it should indicate that the resource pointed to no longer exists.
10+
11+
I believe the HTTP status code: `501 Not implemented` could be used but is not the best solution BUT it could be combined with redirects. Alternatively: `405 Not Allowed` might be a better and with better control with assistance: `Allow` header to solve this:
12+
13+
- You have an API and a resource which can be fetched via: `GET`, it would normally return the resource and the HTTP status code: `200 Ok`
14+
- if the resource was deleted you should return: `410 Gone`
15+
- If the API is deprecated one could return `308 Permanent Redirect` for a period of time indicating the replacement via the `Location` header, like a newer version: `/api/v1` to `/api/v2` and this would work for both: `POST` and `GET` where `301 Moved Permanently` only works for `GET`
16+
- If no replacement is in available return: `405 Not Allowed`
17+
- In due time and after adoption have been made for the new API endpoint, use: `405 Not Allowed` since the API endpoint is gone and it will not come back
18+
19+
For the Swagger part, do see my TIL on that:
20+
21+
- [Deprecating API endpoints](../swagger/deprecating_api_endpoints.md)
22+
23+
## Resources and References
24+
25+
- [MDN: HTTP response status codes](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status)
26+
- [MDN: 301 Moved Permanently](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301)
27+
- [MDN: 302 Found](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/302)
28+
- [MDN: 308 Permanent Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/308)
29+
- [MDN: 405 Method Not Allowed](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/405)
30+
- [MDN: 410 Gone](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/410)
31+
- [MDN: 501 Not Implemented](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/501)
32+
- [StackOverflow: "What is the correct HTTP status code for: "This version of this API has been discontinued"?](https://webmasters.stackexchange.com/questions/71152/what-is-the-correct-http-status-code-for-this-version-of-this-api-has-been-dis)

rest/notes_on_restful_api_design.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Notes on RESTful API Design
2+
3+
This is a list of return codes in relation to a RESTful API design.
4+
5+
Some of the operations has more that one outcome, so you need to decide on what you find the most appropriate one.
6+
7+
<table>
8+
<tr><th>Operation</th><th>HTTP Method</th><th>STATUS CODE ON SUCCESS</th></tr>
9+
<tr><td>Create</td><td>POST</td><td>201 Created</td></tr>
10+
<tr><td></td><td></td><td>202 Accepted</td></tr>
11+
<tr><td>Read</td><td>GET</td></td><td>200 OK</td></tr>
12+
<tr><td rowspan=4>Update</td><td rowspan=2>PUT</td><td>200 OK</td></tr>
13+
<tr><td>204 No Content</td></tr>
14+
<tr><td rowspan=2>PATCH</td><td>200 OK</td></tr>
15+
<tr><td>204 No Content</td></tr>
16+
<tr><td rowspan=3>Delete</td><td rowspan=3>DELETE</td><td>200 OK</td></tr>
17+
<tr><td>202 Accepted</td></tr>
18+
<tr><td>204 No Content</td></tr>
19+
</table>
20+
21+
<table>
22+
<tr><th>Operation</th><th>HTTP Method</th><th>STATUS CODE ON ERROR</th></tr>
23+
<tr><td rowspan=2>Create</td><td rowspan=2>POST</td><td>409 Conflict</td></tr>
24+
<tr><td>422 Unprocessable Content</td></tr>
25+
<tr><td>Read</td><td>GET</td></td><td>404 Not Found</td></tr>
26+
<tr><td rowspan=3>Update</td><td rowspan=2>PUT</td><td>404 Not Found</td></tr>
27+
<tr><td>409 Conflict</td></tr>
28+
<tr><td>PATCH</td><td>404 Not Found</td></tr>
29+
<tr><td>Delete</td><td>DELETE</td><td>404 Not Found</td></tr>
30+
</table>

rest/use_verbs.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,28 @@
11
# Use Verbs
22

3-
Use the verbs specified for the HTTP protocol to get the most our of you HTTP based API.
3+
Use the _verbs_ specified for the HTTP protocol to get the most our of you HTTP based API.
4+
5+
The _verbs_ are more correctly named: HTTP request methods and the term _verbs_ is also widely used.
46

57
- `POST` for creation
68
- `GET` for reading
7-
- `PUT` for updating or replacing
8-
- `PATCH` for updating or modifying
9+
- `PUT` for complete updating or replacing
10+
- `PATCH` for partial updating or modifying
911
- `DELETE` for deletion
1012

11-
If you, _like me_ cannot remember, which of the verbs does what, like `POST` and `PUT` you can use these simple rules of thumb, the first one I got from a colleague:
13+
If you, _like me_ cannot remember, which of the _verbs_ does what, like `POST`, `PUT` and `PATCH` you can use these simple rules of thumb, the first one I got from a colleague:
1214

1315
> The `U` in `PUT` is for _update_
1416
1517
And now for one I had to make for myself:
1618

17-
> Post is always delivered at the gate, so when thinking about `POST`, remember gate rhymes with _create_
19+
> Post is always delivered at the gate, so when thinking about `POST`, remember gate rhymes with _create_...
20+
21+
And the last one is for `PATCH`, if you are RESTful
22+
23+
> `PATCH` can do a partial update compared to `PUT` so "PAT" is short for _partial_...
1824
19-
## Resources
25+
## Resources and References:
2026

2127
- [RestApiTutorial.com](https://www.restapitutorial.com/lessons/httpmethods.html)
28+
- [MDN: HTTP Request Methods](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Methods)

swagger/deprecating_api_endpoints.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ For alternative rendering of a deprecated endpoint, the flag: `deprecated` can b
88
}
99
```
1010

11-
TODO: Write more about other deprecation approaches
11+
For more information on the RESTful part, have a look at my TIL:
12+
13+
- [Deprecating API Endpoints](../rest/deprecating_api_endpoints.md)
1214

1315
## Resources and References
1416

0 commit comments

Comments
 (0)