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.