I am building a REST API. To retrieve a specific product, there is an API endpoint:
GET /product/{product-id}
This endpoint returns 200
with the response body. However, currently, I am returning the status 200
with the body as null when product-id
does not exist. What should be the response code actually? Is returning 200
is ok?
Example:
product with ID 100
, 101
, 102
, 103
exist is my service.
GET /product/100 -> returns json response with status code as 200
GET /product/101 -> returns json response with status code as 200
GET /product/102 -> returns json response with status code as 200
GET /product/103 -> returns json response with status code as 200
But there is no product with ID 999
in this service. What's the response code?
GET /product/999 -> what should be the response code for this case?
This is a very fundamental question for building a basic REST CRUD application. And now I see I don't understand what should be the response code!
I would appreciate it if someone can guide me what is the best practice considering the best practices for REST API design.