0
0
Rest APIprogramming~3 mins

Why 200 OK and 201 Created in Rest API? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your app could instantly tell you exactly what happened after every action?

The Scenario

Imagine you are building a website where users can add new items to a list. Without clear messages, you have to guess if the item was added or if something went wrong.

The Problem

Without proper status codes, you might refresh the page multiple times or get confused if the item was really saved. This wastes time and causes mistakes.

The Solution

Using 200 OK and 201 Created status codes tells you exactly what happened: 200 means the request worked and data is returned, 201 means a new item was successfully created. This clear feedback saves time and avoids errors.

Before vs After
Before
response = send_request()
if response:
    print('Success')
else:
    print('Fail')
After
response = send_request()
if response.status_code == 200:
    print('Data received')
elif response.status_code == 201:
    print('New item created')
What It Enables

Clear communication between client and server, making apps reliable and user-friendly.

Real Life Example

When you post a photo on social media, the server replies with 201 Created to confirm your photo was saved, so you see it instantly in your feed.

Key Takeaways

200 OK means the request succeeded and data is returned.

201 Created means a new resource was successfully made.

Using these codes helps apps work smoothly and clearly.