JSONレスポンスにステータスコードを設定する
JSON本文に201、404などのステータスコードを組み合わせます
「JSONレスポンスにステータスコードを設定する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Status Codes Tell the Story
Every HTTP response carries a status code that signals success or failure. Your JSON body explains the details, but the code sets the tone first. 🚦
The Default Is 200
When you return jsonify alone, Flask sends 200 OK. That is perfect for a successful read, but other actions deserve their own code.
Return a Tuple
To choose a code, return a tuple of the JSON response and the number. Flask reads the second element as the HTTP status to send.
return jsonify(message="created"), 201201 for Created
When a request creates a new record, reply with 201 Created. It tells the client the resource now exists, which 200 alone would not convey.
return jsonify(id=new_id), 201404 for Not Found
If the requested item does not exist, return a JSON body with 404. The client learns the resource is missing, not that the server broke.
return jsonify(error="user not found"), 404400 for Bad Input
When a client sends invalid data, answer with 400 Bad Request. Pair it with a JSON message that explains exactly what went wrong.
return jsonify(error="name is required"), 400The 2xx Family
Codes in the 2xx range mean success. 200 reads, 201 creates, and 204 signals success with no body to send back.
The 4xx Family
Codes in the 4xx range blame the client. 400 means bad input, 401 means unauthenticated, and 403 means forbidden.
The 5xx Family
Codes in the 5xx range blame the server. A 500 means your code raised an unhandled error while processing an otherwise valid request.
Adding Headers Too
You can extend the tuple to three parts: body, status, and a headers dict. That lets you attach extra metadata in one clean return.
return jsonify(ok=True), 201, {"X-Created": "now"}Consistency Builds Trust
Pick the right code for every outcome and use it everywhere. Consistent status codes make your API predictable and a joy to integrate.
Quick Check
Match the action to the most fitting HTTP status code.
Recap
You learned to pair JSON bodies with the right status code by returning a tuple, using 201, 404, and 400 to make your API clear and honest. ✅
よくある質問
「JSONレスポンスにステータスコードを設定する」レッスンは無料ですか?
はい。「JSONレスポンスにステータスコードを設定する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「JSONレスポンスにステータスコードを設定する」で何を学びますか?
JSON本文に201、404などのステータスコードを組み合わせます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「JSONレスポンスにステータスコードを設定する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 辞書をjsonifyする
- リストとネストしたデータを返す
- JSONレスポンスにステータスコードを設定する
- Content-TypeとAcceptヘッダー