REST APIs enable the development of various web applications, supporting all CRUD (Create, Retrieve, Update, Delete) operations. Adhering to REST guidelines involves using specific HTTP methods for different server calls. The following information helps determine the suitable HTTP method for API actions.
Use GET requests to retrieve resource representation/information without modifying it. GET is considered a safe method, and it should be idempotent. The server must return HTTP response code 200 (OK) if the resource is found, 404 (NOT FOUND) if not found, and 400 (BAD REQUEST) for incorrectly formed requests.
- 200 (OK): Resource found, along with the response body (XML or JSON).
- 404 (NOT FOUND): Resource not found.
- 400 (BAD REQUEST): Incorrectly formed GET request.
HTTP GET http://127.0.0.1:5000/api/books/person
HTTP GET http://127.0.0.1:5000/api/books/person/3
import requests
url = "http://127.0.0.1:5000/api/books/person/3" url= "https://sanjayer121.pythonanywhere.com/api/books/person/2" data = { "name": "Shafali khan", "age": 27, "gender": "Male" }
headers = {"Content-Type": "application/json"} response = requests.get(url, headers=headers)
print("Status Code:", response.status_code) print("Response Content:") print(response.json())
Use POST APIs to create new subordinate resources. Responses are not cacheable, and POST is neither safe nor idempotent. Ideally, a successful creation should return HTTP response code 201 (Created).
- 201 (Created): Resource created successfully, with a Location header.
- 200 (OK) or 204 (No Content): Appropriate response if no identifiable URI for the new resource.
HTTP POST http://www.appdomain.com/users
HTTP POST http://www.appdomain.com/users/123/accounts
#--------code---------------
-import requests
-url = "http://127.0.0.1:5000/api/books/person" -url= "https://sanjayer121.pythonanywhere.com/api/books/person/2" -data = {
- "name": "Shafali khan",
- "age": 27,
- "gender": "Male" -}
-headers = {"Content-Type": "application/json"} -response = requests.post(url, json=data, headers=headers)
-print("Status Code:", response.status_code) -print("Response Content:") -print(response.json())
#------------------------------------------
Use PUT APIs to primarily update an existing resource. Responses are not cacheable. If a new resource is created, return HTTP response code 201 (Created). If an existing resource is modified, return either 200 (OK) or 204 (No Content).
- 201 (Created): New resource created.
- 200 (OK) or 204 (No Content): Successful modification of an existing resource.
HTTP PUT http://www.appdomain.com/users/123
HTTP PUT http://www.appdomain.com/users/123/accounts/456
#--------------------code import requests
url = "http://127.0.0.1:5000/api/books/person/3"
data = { "name": "Shafali khan", "age": 27, "gender": "Male" }
headers = {"Content-Type": "application/json"} response = requests.put(url, json=data, headers=headers)
print("Status Code:", response.status_code) print("Response Content:") print(response.json())
#-------------------------------
Note: PUT requests are made on a single resource, while POST requests are made on resource collections.
DELETE APIs delete identified resources. DELETE operations are idempotent, and responses are not cacheable. A successful response should be HTTP response code 200 (OK) with an entity describing the status, 202 (Accepted) if queued, or 204 (No Content) if performed without an entity.
- 200 (OK): Successful deletion with an entity describing the status.
- 202 (Accepted): Action queued.
- 204 (No Content): Action performed without an entity.
HTTP DELETE http://www.appdomain.com/users/123
HTTP DELETE http://www.appdomain.com/users/123/accounts/456
import requests
url = "http://127.0.0.1:5000/api/books/person/3"
headers = {"Content-Type": "application/json"} response = requests.delete(url, headers=headers)
print("Status Code:", response.status_code) print("Response Content:") print(response.json())
Use PATCH requests for partial updates on a resource. PATCH is suitable for updating a part of an existing resource, while PUT is used for replacing a resource entirely.
Note: PATCH support may vary, and request payload is not as straightforward as PUT.
REST API design involves using specific HTTP methods for different actions. Understanding the guidelines for each method helps in building efficient and standardized web applications.
- CRUD: Create, Retrieve, Update, Delete
- Idempotent: Producing the same result every time until modified by another operation.
- Cacheable: Whether the response can be cached by the client.
- URI: Uniform Resource Identifier.