Intern
A GET request to /orders/123 is correctly formed, but such an order does not exist in the database. What status code should be returned? 204 No Content 404 Not Found 410 Gone 403 Forbidden
sobes.tech AI
Answer from AI
The correct status code is 404 Not Found.
Explanation:
- The request is correct, but the resource (order with id 123) is not found in the database.
- 404 means the requested resource does not exist.
Other options:
- 204 No Content — indicates that the request was successful, but the response body is empty; not suitable if the resource does not exist.
- 410 Gone — indicates that the resource was there but has been permanently removed; suitable if it is known that the order has been deleted.
- 403 Forbidden — indicates that access is forbidden; not suitable if there simply is no such order.
Example in Go (using net/http):
func getOrderHandler(w http.ResponseWriter, r *http.Request) {
orderID := "123" // example
order, err := findOrderByID(orderID)
if err == ErrOrderNotFound {
http.NotFound(w, r)
return
}
// process the found order
}