These status codes tell you if your request to a web server worked. They help you understand what happened after you sent data or asked for something.
200 OK and 201 Created in Rest API
HTTP/1.1 200 OK HTTP/1.1 201 Created
200 OK means the request was successful and the server sent back the requested data or confirmation.
201 Created means the request was successful and the server created a new resource as a result.
HTTP/1.1 200 OK Content-Type: application/json {"name": "Alice", "age": 30}
HTTP/1.1 201 Created Location: /users/123 {"id": 123, "name": "Bob"}
This simple web server has two routes: one to get all users with a 200 OK response, and one to add a new user with a 201 Created response.
from flask import Flask, jsonify, request app = Flask(__name__) users = [] @app.route('/users', methods=['GET']) def get_users(): return jsonify(users), 200 @app.route('/users', methods=['POST']) def create_user(): user = request.json user['id'] = len(users) + 1 users.append(user) return jsonify(user), 201 if __name__ == '__main__': app.run(debug=True)
Use 200 OK when you return data or confirm a successful action without creating new resources.
Use 201 Created when your request results in a new resource being made on the server.
Always include useful information in the response body or headers to help the client understand what happened.
200 OK means success and data or confirmation is sent back.
201 Created means success and a new resource was created.
These codes help clients know how their requests were handled.
Practice
200 OK indicate in a REST API response?Solution
Step 1: Understand the meaning of 200 OK
The status code 200 OK means the request was successful and the server is sending back the requested data or confirmation.Step 2: Compare with other codes
201 Created means a new resource was created, which is different from 200 OK.Final Answer:
The request was successful and the server is returning the requested data or confirmation. -> Option BQuick Check:
200 OK = Success with data [OK]
- Confusing 200 OK with 201 Created
- Thinking 200 means resource creation
- Mixing 200 with client or server error codes
Solution
Step 1: Identify the status code for resource creation
The status code 201 Created is used when a new resource is successfully created on the server.Step 2: Eliminate incorrect options
200 OK means success but not necessarily creation; 404 and 500 are error codes.Final Answer:
HTTP/1.1 201 Created -> Option AQuick Check:
201 Created = New resource created [OK]
- Using 200 OK instead of 201 for creation
- Confusing 404 Not Found as success
- Mixing error codes with success codes
HTTP/1.1 201 Created
Location: /users/123
{"id":123,"name":"Alice"}What does this response tell the client?
Solution
Step 1: Analyze the status code 201 Created
This code means a new resource was successfully created on the server.Step 2: Interpret the Location header and response body
The Location header shows where the new resource is located (/users/123), and the body contains the new user's data.Final Answer:
A new user resource was created with ID 123, and its location is provided. -> Option DQuick Check:
201 Created + Location header = New resource created [OK]
- Ignoring the Location header
- Thinking 201 means failure
- Confusing 201 with 200 OK
200 OK instead of 201 Created. What is the likely issue?Solution
Step 1: Understand the difference between 200 OK and 201 Created
200 OK means success but not necessarily resource creation; 201 Created explicitly means a new resource was created.Step 2: Analyze the server behavior
If the resource was created but 200 OK was returned, the server likely forgot to set the correct status code.Final Answer:
The server successfully created the resource but forgot to set the correct status code. -> Option CQuick Check:
201 Created must be set on resource creation [OK]
- Assuming 200 OK means creation
- Ignoring status code importance
- Confusing client errors with server response codes
Solution
Step 1: Identify the correct status code for resource creation
201 Created is the standard code to indicate a new resource was successfully created.Step 2: Use the Location header to provide the new resource URL
Including the Location header with the new post URL helps clients find the resource easily.Final Answer:
Return 201 Created with the Location header set to the new post URL and the post data in the body. -> Option AQuick Check:
201 Created + Location header = Best practice for new resource [OK]
- Returning 200 OK without Location header
- Using error codes for successful creation
- Not including the new resource URL
Ready to prove it?
Pass the test and this pattern moves unknown
