Bird
0
0

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📝 Application Q15 of 15
Rest API - HTTP Status Codes
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?
AReturn <code>201 Created</code> with the Location header set to the new post URL and the post data in the body.
BReturn <code>404 Not Found</code> because the post did not exist before.
CReturn <code>200 OK</code> with the post data in the body but no Location header.
DReturn <code>500 Internal Server Error</code> to indicate the server created the post.
Step-by-Step Solution
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]
Quick Trick: Use 201 Created + Location header for new resources [OK]
Common Mistakes:
MISTAKES
  • Returning 200 OK without Location header
  • Using error codes for successful creation
  • Not including the new resource URL

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes