Bird
Raised Fist0
Rest APIprogramming~10 mins

200 OK and 201 Created in Rest API - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - 200 OK and 201 Created
Client sends HTTP request
↓
Server processes request
↓
Check if resource created?
Yes→Respond with 201 Created
No↓
Respond with 200 OK
↓
Client receives response
The server checks if a new resource was created. If yes, it sends 201 Created; otherwise, it sends 200 OK.
Execution Sample
Rest API
POST /items HTTP/1.1
Host: example.com
Content-Type: application/json

{
  "name": "New Item"
}
Client sends a POST request to create a new item on the server.
Execution Table
StepActionConditionResponse CodeResponse Meaning
1Client sends POST request to /itemsN/AN/AN/A
2Server receives requestN/AN/AN/A
3Server checks if new item createdItem created successfully201Created - new resource made
4Server sends responseN/A201Client knows item was created
5Client receives responseN/A201Client confirms creation
6Client sends GET request to /items/123N/AN/AN/A
7Server processes GET requestResource exists200OK - resource found
8Server sends responseN/A200Client receives resource data
9Client receives responseN/A200Client confirms success
💡 Execution stops after client receives the server response.
Variable Tracker
VariableStartAfter Step 3After Step 7Final
Request TypeNonePOSTGETGET
Resource CreatedFalseTrueTrueTrue
Response CodeNone201200200
Key Moments - 3 Insights
Why does the server send 201 Created instead of 200 OK after a POST?
Because the server successfully created a new resource, it sends 201 Created to tell the client a new item was made (see execution_table row 3).
When does the server send 200 OK instead of 201 Created?
When the client requests existing data (like a GET), the server sends 200 OK to show the request succeeded without creating anything new (see execution_table row 7).
Can 201 Created be used for GET requests?
No, 201 Created is only for successful creation actions like POST. GET requests use 200 OK to indicate success (see execution_table rows 6-9).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what response code does the server send after successfully creating a new item?
A404
B200
C201
D500
💡 Hint
Check row 3 in the execution_table where the server confirms creation.
At which step does the server respond with 200 OK?
AStep 7
BStep 4
CStep 3
DStep 1
💡 Hint
Look at the response codes in the execution_table rows for GET requests.
If the server did not create a new resource after POST, which response code would it most likely send?
A201 Created
B200 OK
C404 Not Found
D400 Bad Request
💡 Hint
Refer to the concept_flow where no creation leads to 200 OK response.
Concept Snapshot
200 OK means the request succeeded and the server returned data.
201 Created means the request succeeded and a new resource was made.
Use 201 after POST creates something new.
Use 200 for successful GET or other requests without creation.
Clients use these codes to understand server actions.
Full Transcript
When a client sends a request to a server, the server processes it and decides what response code to send back. If the client asks to create something new, like with a POST request, and the server successfully creates it, the server sends back 201 Created. This tells the client a new resource was made. If the client asks for existing data, like with a GET request, the server sends 200 OK to show the request succeeded and data is returned. These codes help the client understand what happened on the server side.

Practice

(1/5)
1. What does the HTTP status code 200 OK indicate in a REST API response?
easy
A. A new resource was successfully created on the server.
B. The request was successful and the server is returning the requested data or confirmation.
C. The client made a bad request and the server could not understand it.
D. The server encountered an error and could not complete the request.

Solution

  1. 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.
  2. Step 2: Compare with other codes

    201 Created means a new resource was created, which is different from 200 OK.
  3. Final Answer:

    The request was successful and the server is returning the requested data or confirmation. -> Option B
  4. Quick Check:

    200 OK = Success with data [OK]
Hint: 200 OK means success with data returned [OK]
Common Mistakes:
  • Confusing 200 OK with 201 Created
  • Thinking 200 means resource creation
  • Mixing 200 with client or server error codes
2. Which of the following is the correct way to indicate a successful creation of a new resource in a REST API response?
easy
A. HTTP/1.1 201 Created
B. HTTP/1.1 404 Not Found
C. HTTP/1.1 500 Internal Server Error
D. HTTP/1.1 200 OK

Solution

  1. 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.
  2. Step 2: Eliminate incorrect options

    200 OK means success but not necessarily creation; 404 and 500 are error codes.
  3. Final Answer:

    HTTP/1.1 201 Created -> Option A
  4. Quick Check:

    201 Created = New resource created [OK]
Hint: Use 201 Created for new resource success [OK]
Common Mistakes:
  • Using 200 OK instead of 201 for creation
  • Confusing 404 Not Found as success
  • Mixing error codes with success codes
3. Consider this REST API response code snippet:
HTTP/1.1 201 Created
Location: /users/123

{"id":123,"name":"Alice"}

What does this response tell the client?
medium
A. The client made a bad request and should try again.
B. The request failed and no resource was created.
C. The server is returning existing user data without creating anything new.
D. A new user resource was created with ID 123, and its location is provided.

Solution

  1. Step 1: Analyze the status code 201 Created

    This code means a new resource was successfully created on the server.
  2. 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.
  3. Final Answer:

    A new user resource was created with ID 123, and its location is provided. -> Option D
  4. Quick Check:

    201 Created + Location header = New resource created [OK]
Hint: 201 with Location means new resource created [OK]
Common Mistakes:
  • Ignoring the Location header
  • Thinking 201 means failure
  • Confusing 201 with 200 OK
4. You wrote a REST API endpoint to create a new product. The server returns 200 OK instead of 201 Created. What is the likely issue?
medium
A. The server did not create a new resource but only confirmed the request.
B. The client sent an invalid request format.
C. The server successfully created the resource but forgot to set the correct status code.
D. The server encountered an internal error while creating the resource.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The server successfully created the resource but forgot to set the correct status code. -> Option C
  4. Quick Check:

    201 Created must be set on resource creation [OK]
Hint: 201 must be set when resource is created [OK]
Common Mistakes:
  • Assuming 200 OK means creation
  • Ignoring status code importance
  • Confusing client errors with server response codes
5. You are designing a REST API for a blog. When a new post is created, you want to return the correct status code and the URL of the new post. Which response is the best practice?
hard
A. Return 201 Created with the Location header set to the new post URL and the post data in the body.
B. Return 404 Not Found because the post did not exist before.
C. Return 200 OK with the post data in the body but no Location header.
D. Return 500 Internal Server Error to indicate the server created the post.

Solution

  1. Step 1: Identify the correct status code for resource creation

    201 Created is the standard code to indicate a new resource was successfully created.
  2. 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.
  3. Final Answer:

    Return 201 Created with the Location header set to the new post URL and the post data in the body. -> Option A
  4. Quick Check:

    201 Created + Location header = Best practice for new resource [OK]
Hint: Use 201 Created + Location header for new resources [OK]
Common Mistakes:
  • Returning 200 OK without Location header
  • Using error codes for successful creation
  • Not including the new resource URL
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard