This content originally appeared on DEV Community and was authored by Daniel Trejo
It took me a good while to figure out what I was going to right about but i decided to go with APIs. Today we are going to dive into a subject that at first might seem a little scary but it's actually super easy and essential for building web applications. I'm just going to cover the four fundamental actions you can perform with APIs: POST, PATCH, GET, and DELETE.
Before we jump into the different type of actions, let's cover what an API is. API stands for Application Programming Interface. It allows programs to talk to each other. When coding, you often need your program to interact with other programs or services. APIs is how your going to get that done.
Think of an API like a waiter at a restaurant. You (the user) tell the waiter (API) what you want from the menu (the service), and then the waiter brings back the food (response).
Let’s start with GET.
What It Does: GET is used when you want to retrieve or fetch data from a server. It’s like asking the waiter to bring you a menu or to show you a dish that you’re interested in.
Example: Imagine you’re working on a website that shows user profiles. If you want to display a user’s profile information, you’d use a GET request to fetch that data from the server.
@app.route('/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
user = users.get(user_id)
if user:
return jsonify(user)
else:
return jsonify({'error': 'User not found'}), 404
Next up is POST.
What It Does: POST is used when you want to send new data to the server. It’s like telling the waiter you want to order a new dish.
Example: Let’s say you’re building a sign-up form for a website. When a user fills out the form and hits submit, a POST request is sent to the server to create a new user account with the provided details.
@app.route('/users', methods=['POST'])
def create_user():
new_user = request.json
user_id = max(users.keys()) + 1 if users else 1
users[user_id] = new_user
return jsonify({'id': user_id, **new_user}), 201
Now, let’s talk about PATCH.
What It Does: PATCH is used to update or modify existing data on the server. It’s like asking the waiter to change an ingredient in your dish.
Example: Suppose you want to update the email address of an existing user. You’d use a PATCH request to send only the updated information to the server.
@app.route('/users/<int:user_id>', methods=['PATCH'])
def update_user(user_id):
user = users.get(user_id)
if not user:
return jsonify({'error': 'User not found'}), 404
Finally, we have DELETE.
What It Does: DELETE is used to remove data from the server. It’s like telling the waiter you don’t want a dish anymore and asking them to take it off your table.
Example: If you want to delete a user’s profile, you’d use a DELETE request to remove that user’s data from the server.
@app.route('/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
if user_id in users:
del users[user_id]
return jsonify({'message': 'User deleted'})
else:
return jsonify({'error': 'User not found'}), 404
This content originally appeared on DEV Community and was authored by Daniel Trejo
Daniel Trejo | Sciencx (2024-09-13T12:47:06+00:00) A Beginner’s guide to APIs: Understanding POST, PATCH, GET, and DELETE. Retrieved from https://www.scien.cx/2024/09/13/a-beginners-guide-to-apis-understanding-post-patch-get-and-delete/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.